Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C, check if a file exists without being able to read/write possible? [duplicate]

Tags:

c

fopen

Possible Duplicate:
What’s 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 testfile to "chmod -r somefile". However now it says that the file does not exist even though it does exist.

So i assume if i dont have read permissions i cannot open the file with "fopen r". But that would mean that there is no easy way to tell if a file exists or cannot be read/written?

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!

like image 255
Susan Avatar asked Dec 19 '12 04:12

Susan


People also ask

How do I check whether a file exists without exceptions?

It is a Pythonic way of checking whether a file exists without exception: You simply try to open the file with the built-in open() function: If the file exists without exception it will return the message that files found and if the file doesn't exist then it will raise FileNotFoundError.

How do you check if a file does not exist in C?

access() Function to Check if a File Exists in C Another way to check if the file exists is to use the access() function. The unistd. h header file has a function access to check if the file exists or not. We can use R_OK for reading permission, W_OK for write permission and X_OK to execute permission.

What happens when you try to open a file that doesn't exist in C?

If the file doesn't exist, a new file is created. Returns NULL, if unable to open the file. The file is opened for reading and appending(writing at end of file).

Which of the following is used to check whether file exists or not?

While checking if a file exists, the most commonly used file operators are -e and -f. The '-e' option is used to check whether a file exists regardless of the type, while the '-f' option is used to return true value only if the file is a regular file (not a directory or a device).


1 Answers

fopen actually tries to open the file, which you can't do if you don't have read access. In order to check whether the 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 could get fancier by checking errno if result is 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's EACCESS then you didn't have read permission on one of the directories in the path and so stat can't give you an answer, and so on.

Also, be aware that if you're on a platform with symlinks (any Unix-like, or Windows Vista or later), that you should be aware of whether you are querying about the symlink or the file it points to. If you call stat, then you are asking about the file it points to; if you have a symlink dir/link which points to other/file, then stat will return results about other/file (which is usually what you want, since that's what you would get if you opened the file). However, if you are curious about the link itself (if you want to know "does dir/link exist, even if other/file does not?"), then you should use lstat().

stat() works on Windows as a compatibility wrapper (they prefer you use _stat(), and will warn if you don't), but it's generally better to use the native platform APIs. On Windows, you should probably use GetFileAttributes():

int doesFileExist(const char *filename) {
    return GetFileAttributes(filename) != INVALID_FILE_ATTRIBUTES;
}
like image 105
Brian Campbell Avatar answered Nov 15 '22 23:11

Brian Campbell