After `readFile`, why cannot i `writeFile` ?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

After `readFile`, why cannot i `writeFile` ?

z_axis
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)


Sincerely!
e^(π.i) + 1 = 0
Reply | Threaded
Open this post in threaded view
|

Re: After `readFile`, why cannot i `writeFile` ?

Ivan Lazar Miljenovic
zaxis <[hidden email]> 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
[hidden email]
IvanMiljenovic.wordpress.com
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: After `readFile`, why cannot i `writeFile` ?

Bill Atkins-6
readFile reads the file lazily so it isn't closed until the entire contents have been consumed.

Try System.IO.Strict.readFile, which will read the entire file at once.

zaxis <[hidden email]> 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)

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: After `readFile`, why cannot i `writeFile` ?

z_axis


Bill Atkins-6 wrote
readFile reads the file lazily so it isn't closed until the entire contents have been consumed.

Try System.IO.Strict.readFile, which will read the entire file at once.
Yes, and i can use appendFile too.

appendFile "ssqHitNum.txt" $ unwords $ [no] ++ map (\n -> show n) hitNum ++ ["\n"]

zaxis <z_axis@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)

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
e^(π.i) + 1 = 0
Reply | Threaded
Open this post in threaded view
|

Re: After `readFile`, why cannot i `writeFile` ?

Bill Atkins-6
BTW, "\n -> show n" is equivalent to "show" .

On Monday Jun 7, 2010, at 8:37 PM, zaxis wrote:

>> Yes, and i can use appendFile too.
>>
>> appendFile "ssqHitNum.txt" $ unwords $ [no] ++ map (\n -> show n) hitNum
>> ++ ["\n"]
>

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe