Re: File exist



paytam@xxxxxxxxx opined:

Hi all
Can anyone tell me how can I check that a file exist or no.I mean
when you use this commands
FILE *fp;
if(!fp)
//Could not open the file
doen't show why it can not open it,may be the file doesn't exist.Now
tell me what should I do!

You can try reading `errno` (from <errno.h>) do get to the error code,
provided your implementation gives you one, and it's sensible (it's
not required by the Standard to do that). You'd have to set `errno` to
0 before calling `fopen()`, as library functions never do that. You
should consult documentation that came with your C compiler to see
what `errno` codes are available, and whether they can help you.

If your implementation does set useful error codes, you can then use
`strerror()` from <string.h> to get a huan-readable error string, or
`perror()` from <stdio.h>. Look them up in your manual.

Again, if your implementation does not provide sensible error
codes/messages, you're out in the cold. OTH, relying on your
implementation error codes/messages is more likely than not to make
your code non-portable.

--
Ever heard of .cshrc?
That's a city in Bosnia. Right?
(Discussion in comp.os.linux.misc on the intuitiveness of commands.)

<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>

.



Relevant Pages

  • Re: (Error code -36) copying stuff
    ... The NewGuy wrote: ... Anybody know what (Error code -36) means? ... My solution was to move them using commands in Terminal." ... read posts from Google Groups. ...
    (comp.sys.mac.system)
  • Re: I dont understand the errors on this fork/exec
    ... First error code:: Success ... How come the first perror() prints 'Success'. ... perrorcan change the value of errno. ...
    (comp.unix.programmer)
  • Re: Trying to join an already exited pthread
    ... program are valid for Linux but not for FreeBSD. ... Hence error code 2. ... A component of a specified pathname did not exist, ... I was just being lazy by checking errno, instead of checking the return value of pthread_*. ...
    (freebsd-questions)
  • Re: PTHREAD_SCOPE_PROCESS on Linux
    ... I've tried the following code on Ubuntu 8.04.1 and Fedora 9. ... It dies ... errno does not contain useful information after any pthread_* call, ... the return value from the call is the error code. ...
    (comp.programming.threads)
  • Re: "Try:" which only encompasses head of compound statement
    ... how will you notify it whether the call has succeeded ... the caller itself needs to know. ... except IOError, (ErrNo, Msg): ... (Note how I check for the specific error code I want to handle.) ...
    (comp.lang.python)