doesFileExist 
Author Message
 doesFileExist

main :: IO ()
main = do args <- getArgs
          case args of
            (fileName:rest) -> do
              fileExist <- doesFileExist fileName
              if fileExist then
                do file <- readFile fileName

This little code snippet doesnt seem to work, even though the file does
not exist it tries to read the file.. Can anyone help?

Please reply in mail too.

--

[ gibraltarg. 94-231                         icq: 7540663 ]
[ 412 79 g?teborg                             0707-526663 ]



Sat, 28 Jul 2001 03:00:00 GMT  
 doesFileExist
: main :: IO ()
: main = do args <- getArgs
:           case args of
:             (fileName:rest) -> do
:               fileExist <- doesFileExist fileName
:               if fileExist then
:                 do file <- readFile fileName

: This little code snippet doesnt seem to work, even though the file does
: not exist it tries to read the file.. Can anyone help?

I'm not that familiar yet with programming in Haskell but I think it's
pretty clear what is happening. In unix the first argument to a program is
the name of the program itself. This to allow a program to do different
things when it's called with a different name. Since the program does exist
(you are running it) it tries to read itself. The solution is obvious: read
the second argument:

case args of
  (_:fileName:rest)

This should do the trick.

Frank Meisschaert



Sat, 28 Jul 2001 03:00:00 GMT  
 doesFileExist


: : main :: IO ()
: : main = do args <- getArgs
: :           case args of
: :             (fileName:rest) -> do
: :               fileExist <- doesFileExist fileName
: :               if fileExist then
: :                 do file <- readFile fileName

: I'm not that familiar yet with programming in Haskell but I think it's
: pretty clear what is happening. In unix the first argument to a program is
: the name of the program itself. This to allow a program to do different
: things when it's called with a different name. Since the program does exist
: (you are running it) it tries to read itself. The solution is obvious: read
: the second argument:
: case args of
:   (_:fileName:rest)
: This should do the trick.

I am sorry, but this is not true at all. Have you tried it?
I think your problem is either the doesFileExist function (where is it?)
or the fact that you program refuses to compile.

        n.

--
--[ www.dtek.chalmers.se/~d95mback ]---[ PGP: 0x453504F1 ]---[ UIN: 4439498 ]--



Sun, 29 Jul 2001 03:00:00 GMT  
 doesFileExist

Quote:



> : : main :: IO ()
> : : main = do args <- getArgs
> : :           case args of
> : :             (fileName:rest) -> do
> : :               fileExist <- doesFileExist fileName
> : :               if fileExist then
> : :                 do file <- readFile fileName

> : I'm not that familiar yet with programming in Haskell but I think it's
> : pretty clear what is happening. In unix the first argument to a program is
> : the name of the program itself. This to allow a program to do different
> : things when it's called with a different name. Since the program does exist
> : (you are running it) it tries to read itself. The solution is obvious: read
> : the second argument:
> : case args of
> :   (_:fileName:rest)
> : This should do the trick.

> I am sorry, but this is not true at all. Have you tried it?
> I think your problem is either the doesFileExist function (where is it?)
> or the fact that you program refuses to compile.

doesFileExist is in the Haskell 98 standard library Directory, but I don't
know which implementations support it.  But as you say, a bigger problem for
this program seems to getting it to compile :-)

Kimmo, which compiler/interpreter are you using?

Cheers,

Andy



Sun, 29 Jul 2001 03:00:00 GMT  
 doesFileExist

Quote:


> : main :: IO ()
> : main = do args <- getArgs
> :           case args of
> :             (fileName:rest) -> do
> :               fileExist <- doesFileExist fileName
> :               if fileExist then
> :                 do file <- readFile fileName

> : This little code snippet doesnt seem to work, even though the file does
> : not exist it tries to read the file.. Can anyone help?

> I'm not that familiar yet with programming in Haskell but I think it's
> pretty clear what is happening. In unix the first argument to a program is
> the name of the program itself. This to allow a program to do different
> things when it's called with a different name. Since the program does exist
> (you are running it) it tries to read itself. The solution is obvious: read
> the second argument:

This is not the problem.  In Haskell you get the program name by
calling System.getProgName.  System.getArgs just returns the
arguments after the program name.  

I tried the program with ghc 4.01, and after adding the needed import
statements and an else clause to the if statement, it works fine.
Which Haskell system are you using?

Here's my working version:

import System(getArgs)
import Directory(doesFileExist)

main :: IO ()
main = do args <- getArgs
          case args of
            (fileName:rest) -> do
              fileExist <- doesFileExist fileName
              if fileExist
                then do file <- readFile fileName
                        putStr file
                else putStr (fileName ++ " not found\n")

--
Adam P. Jenkins



Sun, 29 Jul 2001 03:00:00 GMT  
 doesFileExist

Quote:
> fileExist <- doesFileExist fileName

This is a pet peeve of mine: "doesFileExist" is fundamentally bad,
because it guarantees a race condition.  It's *much* better, and
safer, and easier, for openFile to simply return status.  


Sun, 29 Jul 2001 03:00:00 GMT  
 doesFileExist

: > fileExist <- doesFileExist fileName
:
: This is a pet peeve of mine: "doesFileExist" is fundamentally bad,
: because it guarantees a race condition.  It's *much* better, and
: safer, and easier, for openFile to simply return status.  

Or simply just have readFile return Maybe String.

        n.

--
--[ www.dtek.chalmers.se/~d95mback ]---[ PGP: 0x453504F1 ]---[ UIN: 4439498 ]--



Sun, 29 Jul 2001 03:00:00 GMT  
 doesFileExist

 | : > fileExist <- doesFileExist fileName
 | :
 | : This is a pet peeve of mine: "doesFileExist" is fundamentally bad,
 | : because it guarantees a race condition.  It's *much* better, and
 | : safer, and easier, for openFile to simply return status.  

 | Or simply just have readFile return Maybe String.

Or simply raise an exception that can be caught if the programmer is
interested. As it happens now in Haskell.

(Dealing with functions returning a Maybe often gets clutter in your code.
One might say: use monads!, but in the case of "readFile", we are already
in a monad, so why use Maybe at all?).

Regards,
Koen.

--
Koen Claessen,

http://www.cs.chalmers.se/~koen,
Chalmers University of Technology.



Mon, 30 Jul 2001 03:00:00 GMT  
 doesFileExist


Quote:

>> : : main :: IO ()
>> : : main = do args <- getArgs
>> : :           case args of
>> : :             (fileName:rest) -> do
>> : :               fileExist <- doesFileExist fileName
>> : :               if fileExist then
>> : :                 do file <- readFile fileName
> doesFileExist is in the Haskell 98 standard library Directory, but I don't
> know which implementations support it.  But as you say, a bigger problem for
> this program seems to getting it to compile :-)

> Kimmo, which compiler/interpreter are you using?

As I said it was just a snippet from a larger bit of code so of course I get
it to compile. Otherwise it would be a kind of stupid question =) . And I
import Directory. I use hbc.

I tried Adam P. Jenkins small version that he says works for him.

--8<--

import System(getArgs)
import Directory(doesFileExist)

main :: IO ()
main = do args <- getArgs
          case args of
            (fileName:rest) -> do
              fileExist <- doesFileExist fileName
              if fileExist
                then do file <- readFile fileName
                        putStr file
                else putStr (fileName ++ " not found\n")
--8<--

And it doesn't work for me in hbc at least.

Any other ides anyone?

--

[ gibraltarg. 94-231                         icq: 7540663 ]
[ 412 79 g?teborg                             0707-526663 ]



Sun, 05 Aug 2001 03:00:00 GMT  
 
 [ 9 post ] 

 Relevant Pages 
 

 
Powered by phpBB® Forum Software