check if file exists c code example
Example 1: c check if file exists
if( access( fname, F_OK ) == 0 ) {
// file exists
} else {
// file doesn't exist
}
Example 2: c check dir/file
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int is_regular_file(const char *path)
{
struct stat path_stat;
stat(path, &path_stat);
return S_ISREG(path_stat.st_mode);
}
Example 3: c check if file was created
int canCreateFile(char* path)
{
FILE* file = fopen(path, "w");
if(file)
{
fclose(file);
return 1;
}
return 0;
}