C File Exists

Summary: in this tutorial, you will learn how to develop a C file exists function that checks if a file exists using various C standard library functions.

Sometimes, you want to check if a file exists before reading from or writing to the file. However, C does not provide any standard function to check if a file exists.

Fortunately, you can use other functions from the standard library to develop your own function check if a file exists or not. The functions that we’ll use to check for the existence of a file are:

  • fopen()
  • stat()
  • access()

Check if a file exists using the fopen() function

In the first approach, you will try to read the data from the file using the fopen() function. If you can read data from the file, it means that file exists otherwise it does not.

The following illustrates how to use the fopen() function to check if a file exists:

#include <stdio.h> #include <stdbool.h> // return true if the file specified by the filename exists bool file_exists(const char *filename) { FILE *fp = fopen(filename, "r"); bool is_exist = false; if (fp != NULL) { is_exist = true; fclose(fp); // close the file } return is_exist; } int main() { char *filename = "readme.txt"; if (file_exists(filename)) printf("File %s exists", filename); else printf("File %s doesn't exist.", filename); return 0; }
Code language: C++ (cpp)

The function accepts a file name and returns true if the file exists. Otherwise, it returns 0.

Check if a file exists using stat() function

The stat() function reads all the properties of a file including the file size, creation date, and modified date. The stat() function return -1 if the file doesn’t exist or zero (0) otherwise. The stat() function is included in the ys/stat.h standard library.

The following shows how to use the stat() function to check if a file exists:

#include <stdio.h> #include <sys/stat.h> #include <stdbool.h> // return true if the file specified // by the filename exists bool file_exists(const char *filename) { struct stat buffer; return stat(filename, &buffer) == 0 ? true : false; } int main() { char *filename = "readme.txt"; if (file_exists(filename)) printf("File %s exists", filename); else printf("File %s doesn't exist.", filename); return 0; }
Code language: C++ (cpp)

Check if a file exists using the access() function

The access() function checks if a file can be read/write/executed by a user. It can be used to check for the existence of a file:

int access (const char *filename, int how)
Code language: JavaScript (javascript)

The *filename is the path to a file that you want to check. The valid value for the how flag is

  • R_OK – test for the read permission
  • W_OK – test for the write permission
  • X_OK – test for the execute permission
  • F_OK – test for the existence of the file.

The access() function returns 0 if the access is permitted, or -1 otherwise.

The following program illustrates how to use the access() function to check for the existance of a file:

#include <stdio.h> #include <unistd.h> #include <stdbool.h> // return true if the file specified // by the filename exists bool file_exists(const char *filename) { return access(filename, F_OK) == 0; } int main() { char *filename = "readme.txt"; if (file_exists(filename)) printf("File %s exists", filename); else printf("File %s doesn't exist.", filename); return 0; }
Code language: PHP (php)

Summary

  • Use the fopen() function to check if a file exists by attempting to read from it.
  • Use the stat() function to check if a file exists by attempting to read properties from the file.
  • Use the access() function with the F_OK flag to check if a file exists.
Was this tutorial helpful ?