Cookbook/Files
From HaskellWiki
(Difference between revisions)
(smaller headlines) |
|||
| (2 intermediate revisions not shown.) | |||
| Line 1: | Line 1: | ||
| - | = Reading from a file = | + | == Reading from a file == |
The System.IO library contains the functions needed for file IO. The program | The System.IO library contains the functions needed for file IO. The program | ||
below displays the contents of the file c:\test.txt. | below displays the contents of the file c:\test.txt. | ||
| Line 10: | Line 10: | ||
contents <- hGetContents h | contents <- hGetContents h | ||
putStrLn contents | putStrLn contents | ||
| - | |||
</haskell> | </haskell> | ||
| - | The same program, with some higher- | + | ''Note: It is important never to call ''<code>hClose</code>'' on a file-handle which has had ''<code>hGetContents</code>'' run on it already. The file handle is in a semi-closed state, and will be closed when the resulting string is garbage collected. Closing it manually may result in a random truncation of the input.'' |
| + | |||
| + | The same program, with some higher-level functions: | ||
<haskell> | <haskell> | ||
| Line 21: | Line 22: | ||
</haskell> | </haskell> | ||
| - | = Writing to a file = | + | == Writing to a file == |
The following program writes the first 100 squares to a file: | The following program writes the first 100 squares to a file: | ||
| Line 35: | Line 36: | ||
This will override the old contents of the file, or create a new file if the file doesn't exist yet. If you want to append to a file, you can use <hask>appendFile</hask>. | This will override the old contents of the file, or create a new file if the file doesn't exist yet. If you want to append to a file, you can use <hask>appendFile</hask>. | ||
| - | = Creating a temporary file = | + | == Creating a temporary file == |
| - | TODO | + | TODO: abstract via 'withTempFile', handle exception |
| + | |||
| + | <haskell> | ||
| + | import System.IO | ||
| + | import System.Directory | ||
| + | |||
| + | main = do | ||
| + | tmpDir <- getTemporaryDirectory | ||
| + | (tmpFile, h) <- openTempFile tmpDir "foo" | ||
| + | hPutStr h "Hello world" | ||
| + | hClose h | ||
| + | removeFile tmpFile | ||
| + | </haskell> | ||
| - | = Writing a filter | + | == Writing a filter == |
Using [http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:interact interact], you can easily do things with stdin and stdout. | Using [http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:interact interact], you can easily do things with stdin and stdout. | ||
| Line 54: | Line 67: | ||
</haskell> | </haskell> | ||
| - | = Logging to a file = | + | == Logging to a file == |
TODO | TODO | ||
Revision as of 11:33, 9 July 2009
Contents |
1 Reading from a file
The System.IO library contains the functions needed for file IO. The program below displays the contents of the file c:\test.txt.
import System.IO main = do h <- openFile "c:\\test.txt" ReadMode contents <- hGetContents h putStrLn contents
Note: It is important never to call hClose on a file-handle which has had hGetContents run on it already. The file handle is in a semi-closed state, and will be closed when the resulting string is garbage collected. Closing it manually may result in a random truncation of the input.
The same program, with some higher-level functions:
main = do contents <- readFile "c:\\test.txt" putStrLn contents
2 Writing to a file
The following program writes the first 100 squares to a file:
-- generate a list of squares with length 'num' in string-format. numbers num = unlines $ take num $ map (show . \x -> x*x) [1..] main = do writeFile "test.txt" (numbers 100) putStrLn "successfully written"
appendFile
3 Creating a temporary file
TODO: abstract via 'withTempFile', handle exception
import System.IO import System.Directory main = do tmpDir <- getTemporaryDirectory (tmpFile, h) <- openTempFile tmpDir "foo" hPutStr h "Hello world" hClose h removeFile tmpFile
4 Writing a filter
Using interact, you can easily do things with stdin and stdout.
A program to sum up numbers:
main = interact $ show . sum . map read . lines
A program that adds line numbers to each line:
main = interact numberLines numberLines = unlines . zipWith combine [1..] . lines where combine lineNumber text = concat [show lineNumber, " ", text]
5 Logging to a file
TODO
