Thursday 26 May 2016

Haskell: writeFile: Write string to a file

System.IO module provides writeFile function takes a filename and string as arguments and write it to a file. If the file is not exist, it creates one, If the file is already exist, it overrides the contents of the file.

Prelude System.IO> :t writeFile
writeFile :: FilePath -> String -> IO ()


FileUtil.hs
import System.IO

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

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

$ ghc FileUtil.hs
Linking FileUtil ...
$
$ ./FileUtil
Enter file name
today.txt
Enter some data to write to file
Hello ptr
How r u doing
Have a good day
tacke care bye bye
$
$ cat today.txt
Hello ptr
How r u doing
Have a good day
tacke care bye bye



Previous                                                 Next                                                 Home

No comments:

Post a Comment