Friday, 27 May 2016

Haskell: appendFile: Append data to file

System.IO module provides appendFile function which takes file name and a string as arguments and append the string to given file. If the file is not exist already, it creates one.


FileUtil.hs
import System.IO

main = do
        putStrLn "Enter file name"
        fileName <- getLine

        putStrLn "Enter some data to write to file"
        information <- getContents
     
        appendFile fileName information

$ cat today.txt
Hello ptr
How r u doing
Have a good day
tacke care bye bye
$
$ ghc FileUtil.hs
[1 of 1] Compiling Main             ( FileUtil.hs, FileUtil.o )
Linking FileUtil ...
$
$ ./FileUtil
Enter file name
today.txt
Enter some data to write to file
Ya Hari
see u bye bye
$
$ cat today.txt 
Hello ptr
How r u doing
Have a good day
tacke care bye bye
Ya Hari
see u bye bye


Previous                                                 Next                                                 Home

No comments:

Post a Comment