. Advertisement .
..3..
. Advertisement .
..4..
Still trying to find ways to check if files exist in C? We will solve this problem for you. Our guide has provided several ways to perform this task in C programming. Check it out and follow our guidance.
Method 1: Check If Files Exist C Utilizing fope()
For example, name the file ittutoria.txt
. This file ittutoria.txt
and program C are both in one directory. Thus, file exists is the output.
Suppose the location of the file and the program C differ; the entire path to the file must be specified.
To determine whether the file is there or not, you may construct a C user-defined function. Here is an example of a program with the C user-defined feature.
#include<stdio.h>
int checkIfFileExists(const char *filename);
int main(void)
{
if(checkIfFileExists("ittutoria.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;
}
If the file ittutoria.txt
and program C are located in the same place, this program output file exists. On the other hand, you will have to specify the entire path of the file if the location of the file and the program differ.
Method 2: Check Whether Files Exist C Utilizing stat()
You can use the function stat()
to read the properties of a file rather than its data. This approach will give back 0 when the procedure is successful. On the contrary, you will receive a -1 value in case the file doesn’t exist in C.
#include<stdio.h>
#include<sys/stat.h>
int checkIfFileExists(const char *filename);
int main(void)
{
if(checkIfFileExists("ittutoria.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;
}
Suppose both the program C and the file ittutoria.txt
are in one place; you will get an output file exists. If not, you will have to specify the entire path of the file.
Method 3: Check Whether Files Exist C Utilizing access()
Using the function access()
is another method of determining whether a file is present. The feature access to determine whether the file is there or not can be found in the header file unistd.h.
R_OK
, W_OK
, and X_OK
can be used to grant reading, writing, and execution rights, respectively. You can also utilize R_OK|W_OK
for writing and reading permission.
#include<stdio.h>
#include<unistd.h>
int main(void)
{
if( access( "C:\\TEMP\\ittutoria.txt", F_OK ) != -1)
{
printf("file is found");
}
else
{
printf("file is not found");
}
return 0;
}
Output:
file is found
In this case, the location of the file is C:\TEMP\ittutoria.txt
. Suppose the file is present; it will print the output file is found. If not, the result is file is not found. Again, the file ittutoria.txt
and the program must be in one place. Or else, you must specify the file’s location. For the Linux OS (operating system), the access()
and stat()
methods are an excellent combo.
The following is another technique to employ the function access()
.
#include <stdio.h>
#include <stdlib.h>
#include<unistd.h>
void checkIfFileExists(const char *fileName);
int main (void) {
char *fileName = "C:\\TEMP\\ittutoria.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);
}
}
Output:
The file C:\TEMP\ittutoria.txt was found
The file C:\TEMP\ittutoria.txt can be read
The file C:\TEMP\ittutoria.txt can be Edited
The file C:\TEMP\ittutoria.txt is an executable
The Bottom Line
Above are three methods you can apply to check if files exist in C. Opening files in writing or reading mode will allow you to see if they already exist. Hopefully, you will find our tutorial easy to understand and follow.
Bonus: If you want to learn more about C programming, check out our guide on getting a substring in C.
Leave a comment