C Program to Check Whether a File Exists or Not

In this article, we will write a C program to check whether a file exists or not in a given directory.

The program asks the user to enter the file name or file path, checks if the file exists or not, and prints the result on the output window.

Sample Input:

Enter the file name or file path: data/myfile.txt

Sample Output:

File Exists

The easiest way to check if a file exists or not in C programming is to use the built-in fopen() function.

The fopen() function is a part of the standard input-output library(stdio.h). It takes the name or path of the file which needs to be checked as a first parameter and the mode in which we want to open the file as a second parameter.

FILE *fopen(const char *filename, const char *mode);

The fopen() function returns a FILE pointer. If the specified file exists, the fopen() function returns a file pointer which by default points to the beginning of the file and if the file does not exist it returns a NULL pointer.

We can use this concept to check if the given file exists or not.

// C program to check whether
// a given file exists or not

#include<stdio.h>

int main(){
	
	char filename[100];
	
	printf("Enter the file name or file path: ");
	scanf("%s", &filename);
	
	// Try to open the file in read mode
	FILE *fptr = fopen(filename, "r");
	
	if(fptr == NULL){
		printf("File does not exist");
	}
	else{
		printf("File Exists");
		
		// Close the file
		fclose(fptr);
	}
		
    return 0;
    
}

Output:

Enter the file name or file path: temp.txt
File Exists

Enter the file name or file path: data/myfile.txt
File Exists

Enter the file name or file path: myfile.txt
File does not exist

At first glance, the above program might look reliable to check the file’s existence. But in reality, it’s not.

That’s because, if the given file does exist but because of some reasons the program is not able to open the file for eg. file is locked by another process or there are some access issues then in that case also, the fopen() function will return a NULL pointer and you will get output that the file does not exist.


Method 2: Using access() Function to Check File Existence

The access() function is way more reliable than the fopen() function to check the existence of a file.

The access() function is a part of the <unistd.h> library and it is primarily used to check the accessibility and permissions of files and directories.

It has the following syntax:

int access(const char *pathname, int mode);

If the access() function returns 0 it means that the file or directory is accessible as per the specified mode.

But if it returns -1, it means that there is a failure to access the file. It can be because of access permission or because the specified file does not exist.

The following program shows how we can use it to check if the given file exists or not:

// C program to check whether
// a given file exists or not

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

int main(){
	
	char filename[100];
	
	printf("Enter the file name or file path: ");
	scanf("%s", &filename);
	
	if(access(filename, F_OK) == 0){
		printf("File Exists");
	}
	else{
		printf("File does not exist");
	}
		
    return 0;
    
}

Output:

Enter the file name or file path: temp.txt
File Exists

Enter the file name or file path: data/myfile.txt
File Exists

Enter the file name or file path: random.txt
File does not exist

Method 3: Using stat() Function to Check File Existence

There is one more way to check if a given file exists or not in C, the stat() function.

The stat() function is a part of the <sys/stat.h> header library and it is primarily used to get the metadata of a file such as its size, its creation date, updation date, etc.

It has the following syntax:

int stat(const char *pathname, struct stat *statbuf);

To check if a file exists or not using the stat() function, we can use its returned value.

If the stat() function returns 0, it means the file exists and if it returns -1, it means that the file does not exist.

The following C program shows how you can check a file’s existence using the stat() function:

// C program to check whether
// a given file exists or not

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

int main(){
	
	char filename[100];
	struct stat fileStats;
	
	printf("Enter the file name or file path: ");
	scanf("%s", &filename);
	
	
	
	if(stat(filename, &fileStats) == 0){
		printf("File Exists");
	}
	else{
		printf("File does not exist");
	}
		
    return 0;
    
}

Output:

Enter the file name or file path: myfile.txt
File does not exist

Enter the file name or file path: temp.txt
File Exists

Enter the file name or file path: data/myfile.txt
File Exists

That’s all for this article. Thanks for reading!

Author

  • Manoj Kumar

    Hi, My name is Manoj Kumar. I am a full-stack developer with a passion for creating robust and efficient web applications. I have hands-on experience with a diverse set of technologies, including but not limited to HTML, CSS, JavaScript, TypeScript, Angular, Node.js, Express, React, and MongoDB.

    View all posts