I need to create a function that checks if a file exists. My intention is to do something like: teste :: String -> Bool (Beeing the String the FilePath) I've tried to use openFile and catch functions, but i couldn't put it working. The purpose of this function is to test a file before trying to read it. Example: if (test "filepath") then readfile else fail Thank you very much for your help, Joao ------------------------------------ geek code inside :: powered by linux
bracaman@box.sk wrote:
I need to create a function that checks if a file exists. My intention is to do something like:
teste :: String -> Bool (Beeing the String the FilePath)
I've tried to use openFile and catch functions, but i couldn't put it working. The purpose of this function is to test a file before trying to read it. Example:
if (test "filepath") then readfile else fail
This kind of function, which tests the possibility of something in the world, is generally bad. The reason is that the things might change between the point where yout test for a file being readable and the time when you actually read it. And then your program will fail anyway. It's much better to make it work with the atomic readfile operation. -- Lennart
I am trying to emulate bash in haskell, and i got a function called shell that waits fot the command and then executes it. The problem is that if the file does not exist, the program returns to Main (it gets out of the shell). The only thing i want to do is to return to the shell after an IOError. How can i perform such a thing? Thank you, Joao On Sat, Nov 03, 2001 at 10:56:38AM +0000, bracaman@box.sk wrote:
I need to create a function that checks if a file exists. My intention is to do something like:
teste :: String -> Bool (Beeing the String the FilePath)
I've tried to use openFile and catch functions, but i couldn't put it working. The purpose of this function is to test a file before trying to read it. Example:
if (test "filepath") then readfile else fail
Thank you very much for your help,
Joao
------------------------------------ geek code inside :: powered by linux
_______________________________________________ Haskell mailing list Haskell@haskell.org http://www.haskell.org/mailman/listinfo/haskell
-- ------------------------------------ geek code inside :: powered by linux
bracaman@box.sk writes:
I am trying to emulate bash in haskell, and i got a function called shell that waits fot the command and then executes it. The problem is that if the file does not exist, the program returns to Main (it gets out of the shell). The only thing i want to do is to return to the shell after an IOError.
How can i perform such a thing?
See the documentation at: http://haskell.org/ghc/docs/latest/set/sec-exception.html particularly the example: catch (openFile f ReadMode) (\e -> hPutStr stderr ("Couldn't open "++f++": " ++ show e)) If GHCi could load the Posix module, that would make a very interesting shell, but GHCi does not seem to have such ability yet. Tim -- "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -- Benjamin Franklin
participants (3)
-
bracaman@box.sk -
Lennart Augustsson -
Tim Barbour