Geiregat Jonas said:
is using
if(open("file",O_EXCL) != -1){ printf("File does
exists")}else{printf("file does not exists"); }
a good way of checking if a file exists or not, if not how should I do it ?
There's no really good portable way to determine whether a named file
exists; you'll probably have to resort to system-specific methods
(which you can ask about in another newsgroup).
Before you do, you should define more clearly what information you're
looking for and what you intend to do with it. On a multi-processing
system, for example, if your logic is something like this:
if (file exists) {
open existing file
}
else {
create new file
initialize new file
}
you should allow for the possibility that the file is created <OT>by
another process</OT> after you check for its existence and before you
attempt to create it.
Note also that if a file doesn't exist, that doesn't imply that you
have permission to create it.
In many cases, it may not be possible to determine whether a file
exists. On a system with user accounts and directories with
permission settings, an attempt to determine whether a file exists may
fail because you don't have permission to read the directory that may
or may not contain it.