How to Check if a File Exists in C

  1. fopen() Function to Check if a File Exists in C
  2. stat() Function to Check if a File Exists in C
  3. access() Function to Check if a File Exists in C

This tutorial introduces how to check if the file exists in C. The way to check if a file exists is to try opening the file in reading or writing mode. This program is opening a file in reading mode.

fopen() Function to Check if a File Exists in C

#include<stdio.h>
int main(void)
{
    FILE *file;
    
    if (file = fopen("demo.txt", "r")) 
    {
        fclose(file);
        printf("file exists");
    }
    else
    {
        printf("file doesn't exist");
    }
    
    return 0;
}

The output is:

file exists

Here the file name is demo.txt. The C program and demo.txt file are in the same directory. So the output is file exists.

If the C program location and file location are different, we must specify the file’s full path.

We can create a user-defined function and check if the file exists or not. Below is the program with a user-defined function.

#include<stdio.h>
int checkIfFileExists(const char *filename);
int main(void)
{
    if(checkIfFileExists("demo.txt"))
    {
        printf("file exists");
    }
    else
    {
        printf("file does not exists");
    }
}

int checkIfFileExists(const char * filename)
{
    FILE *file;
    if (file = fopen(filename, "r"))
    {
        fclose(file);
        return 1;
    }

    return 0;
}

The program will print file exists if the demo.txt file and C program have the same location. If the C program and the file name are at different locations, we must specify the file’s full path.

stat() Function to Check if a File Exists in C

We read the file’s attributes using the stat() function instead of reading data from a file. This function will return 0 if the operation is successful; otherwise, it will return -1, if the file does not exist.

#include<stdio.h> 
#include<sys/stat.h>

int checkIfFileExists(const char *filename);

int main(void)
{
    if(checkIfFileExists("demo.txt"))
    {
        printf("file exists");
    }
    else
    {
        printf("file does not exists");
    }

}

int checkIfFileExists(const char* filename){
    struct stat buffer;
    int exist = stat(filename,&buffer);
    if(exist == 0)
        return 1;
    else  
        return 0;
}

The program will print file exists if the demo.txt file and C program have the same location. If the C program and the file name are at different locations, we must specify the file’s full path.

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. We can use them together as R_OK|W_OK to read and write permission.

#include<stdio.h>
#include<unistd.h>
int main(void)
{
    if( access( "C:\\TEMP\\demo.txt", F_OK ) != -1)
    {
        printf("file is found");
    }
    else
    {
    printf("file is not found");
    }
    return 0;
}

The output is:

file is found

Here, C:TEMPdemo.txt is the file location. If the file exists, it prints file is found; otherwise, it prints file is not found. The program location and the demo.txt file have the same location. Otherwise, we have to specify the location of the file. The stat and access function is a good choice for the Linux operating system.

Another way to use access function is as below:

#include <stdio.h>
#include <stdlib.h>
#include<unistd.h>

void checkIfFileExists(const char *fileName);

int main (void) {
    char *fileName = "C:\\TEMP\\demo.txt";

    checkIfFileExists(fileName);
    return 0;

}

void checkIfFileExists(const char *fileName){

    if(!access(fileName, F_OK )){
        printf("The File %s was Found\n",fileName);
    }else{
        printf("The File %s not Found\n",fileName);
    }
    
    if(!access(fileName, R_OK )){
        printf("The File %s can be read\n",fileName);
    }else{
        printf("The File %s cannot be read\n",fileName);
    }
    
    if(!access( fileName, W_OK )){
        printf("The File %s  can be Edited\n",fileName);
    }else{
        printf("The File %s  cannot be Edited\n",fileName);
    }
    
    if(!access( fileName, X_OK )){
        printf("The File %s is an Executable\n",fileName);
    }else{
        printf("The File %s is not an Executable\n",fileName);
    }

}

The output is:

The file C:\TEMP\demo.txt was found
The file C:\TEMP\demo.txt can be read
The file C:\TEMP\demo.txt can be Edited
The file C:\TEMP\demo.txt is an executable
comments powered by Disqus