[Haskell-cafe] After `readFile`, why cannot i `writeFile` ?

Ivan Lazar Miljenovic ivan.miljenovic at gmail.com
Mon Jun 7 18:53:06 EDT 2010


zaxis <z_axis at 163.com> writes:

> hitSSQ :: String -> [Int] -> IO ()
> hitSSQ no hitNum = do
>     let newNum = unwords $ [no] ++ map (\n -> show n) hitNum
>     hitNums <- fmap lines $ readFile "test.txt"
>     writeFile "test.txt" $ unlines $ hitNums ++ [newNum]
>
> *** Exception: test.txt: openFile: resource busy (file is locked)

You're currently reading the file; finish reading it and close it before
you write over it (or else use WriteRead mode or whatever it's called
and call openFile explicitly rather than using readFile and writeFile).

-- 
Ivan Lazar Miljenovic
Ivan.Miljenovic at gmail.com
IvanMiljenovic.wordpress.com


More information about the Haskell-Cafe mailing list