Question:
As I work my way through Learn You as Haskell, I have been writing small File IO programs.
Unlike the example in that book, where the author reads from one file, writes altered data to a temp file and then deletes the original file and renames the temp file, I have been trying to read from a given file, alter the data and then overwrite the same file with the new data. Is this possible?
In a main do block I have tried:
contents <- readFile fileName
-- do stuff to contents--
writeFile fileName result
After using appendFile to add items to the file, when I call the function to read and write I get this error:
openFile: resource busy, (file is locked)
I have also tried using the file handles like so:
handle <- openFile fileName ReadWriteMode
contents <- hGetContents handle