Let’s say that the user input = 6000 and the number inside input.txt is = 5000. The sum up will be 11000. The number that will be displayed on the screen and the value stored in the file will be overwritten to 11000. Please help me, Thanks
import System.IO
menu :: IO ()
menu = do
handle <- openFile "input.txt" ReadMode
contents <- hGetContents handle
putStr contents
hClose handle
contents <- readFile "input.txt"
print . sum . read $ contents
putStr("\nThe amount of money that you want to deposit : ")
y<-getLine
Whats wrong with yor code :
Lots of problems with your code.
It is better to see types of functions you are using and then compose them.
A probable correction of your code from what I can infer you want would be
Direct answer :
It is better to use
withFileas you want to read and then write.readFilereads the file lazily so there is no guarantee when the file handle will be closed. In the above case if you write thewriteFileline before printing the output it might give a runtime error complaining about open handles. SowithFileis a safer option and you will be opening file only once as compared to twice in the above case.I have considered that you just want to add the number in the first line of the input file with the number you input.