C, check if the file exists with no read / write capability?

Possible duplicate:
What is the best way to check if a file exists in C? (cross platform)

I would like to check if a file exists or not. I changed the permissions of my test file to "chmod -r somefile". However, he now says that the file does not exist, even if it exists.

So, I assume that if I do not have read permissions, I cannot open the file with "fopen r". But this would mean that there is no easy way to determine if a file exists or cannot be read / write?

Or am I missing something? Any help would be great.

Thanks!

int doesFileExist(const char* filename) { FILE* fptr = fopen(filename, "r"); if (fptr != NULL) { fclose(fptr); printf("File exists\n"); return 1; } printf("File doesnt exist\n"); return 0; } 

Update: Thanks guys for these great links and explanations!

+7
source share
3 answers

fopen actually trying to open a file that you cannot make if you do not have read access. To check if a file exists without opening it, use stat ; stat gives you metadata about the file and requires only read access to the directory containing the file, not the file itself.

 int doesFileExist(const char *filename) { struct stat st; int result = stat(filename, &st); return result == 0; } 

You can fall in love by checking errno if result not 0; if errno is ENOENT , then the file does not exist, if it is ENOTDIR , then part of the path you provided is not a directory, if it is EACCESS , then you did not have permission to read on one of the directories in the path, and therefore stat cannot give you an answer etc.

Also, keep in mind that if you are on a platform with symbolic links (any Unix-like or Windows Vista or later), you should know if you are requesting a link to a symbolic link or the file to which it points. If you call stat , then you ask about the file that it points to; if you have a dir/link symbolic link that points to other/file , then stat will return the results about other/file (usually this is what you want, since this is what you would get if you opened the file). However, if you are interested in learning about the link itself (if you want to know if dir/link exists, even if there is no other/file ? "), Then you should use lstat() .

stat() works on Windows as a compatibility shell (they prefer to use _stat() and will warn you if you donโ€™t), but itโ€™s usually better to use platform-based APIs. On Windows, you should probably use GetFileAttributes() :

 int doesFileExist(const char *filename) { return GetFileAttributes(filename) != INVALID_FILE_ATTRIBUTES; } 
+18
source

You really don't check if the file exists here ... You check if you can open it with read access. This may be unsuccessful for reasons other than a file that does not exist. As you saw, you may not have read permissions. It may also be that the file is locked.

Mark this answer:

What is the best way to check if a file exists in C? (cross platform)

It is suggested to use stat , which should work in most cases, except when you do not have read access to the directory in which the file is located.

+1
source

On Linux (and many other systems), you can use opendir() and friends to display directories. I think only read-only files will be available. Other platforms should have similar features.

-one
source

All Articles