C program to check whether a file or directory exists or not

Write a C program to verify whether or not a file or listing exists or not. Methods to verify file or listing exists or not in C programming. What's the easiest way to verify file or listing exists or not in C programming.

Required data

Fundamental Enter Output, File dealing with

Methods to verify if file exists or not?

In contrast to different fashionable programming languages like Java or C#, C doesn't have any direct built-in library perform to verify file or listing existence. Nevertheless, nonetheless there are numerous methods to verify if a file exists or not.

C helps 3 ways to verify existence of a file on disk. I'll clarify them one after the other.

  1. Utilizing fopen() perform

    We have now used fopen() perform a number of occasions via the course of file dealing with workout routines. fopen() returns pointer to FILE sort on success in any other case NULL.

    Article post on: dinhthienbao.com

    You should utilize fopen() perform to open given file in learn mode. If it returns NULL then file doesn't exists in any other case exists on disk.

    Testing file existence utilizing fopen() will not be dependable. fopen() fails if you do not have learn/write/execute permissions on file. In such case additionally fopen() returns NULL, however file exists.

  2. Utilizing entry() perform

    int entry(const char *path, int amode); perform is outlined in unistd.h header file. It's used to verify accessibility of a file. It's extra dependable to verify file existence.

    Source: dinhthienbao.com

    It accepts two parameters, first *path is a pointer to fixed character pointing to file path. Subsequent is amode which is a bit sample outlined by one or mixture of F_OK, R_OK, W_OK and X_OK constants.

    It returns Zero if file has requested entry amode, in any other case -1.

  3. Utilizing stat() perform

    Most likely this isn't the easiest way to verify file existence. However if in case you have stat construction object then you can too use it for checking file existence or verify different file permissions.

    Via @: dinhthienbao.com

    st_mode subject of stat construction incorporates bit sample specifying permissions on file. If F_OK bit is ready on st_mode subject then file exists in any other case not.

Program to verify whether or not a file exists or not

/** * C program to verify whether or not a file exists or not. */ #embrace  #embrace  #embrace  #embrace  int isFileExists(const char *path); int isFileExistsAccess(const char *path); int isFileExistsStats(const char *path); int essential() { char path[100]; printf("Enter supply file path: "); scanf("%s", path); // Examine if file exists or not if (isFileExistsAccess(path)) { printf("File exists at path '%s'n", path); } else { printf("File doesn't exists at path '%s'n", path); } return 0; } /** * Perform to verify whether or not a file exists or not. * It returns 1 if file exists at given path in any other case * returns 0. */ int isFileExists(const char *path) { // Attempt to open file FILE *fptr = fopen(path, "r"); // If file doesn't exists if (fptr == NULL) return 0; // File exists therefore shut file and return true. fclose(fptr); return 1; } /** * Perform to verify whether or not a file exists or not utilizing * entry() perform. It returns 1 if file exists at * given path in any other case returns 0. */ int isFileExistsAccess(const char *path) { // Examine for file existence if (entry(path, F_OK) == -1) return 0; return 1; } /** * Perform to verify whether or not a file exists or not utilizing * stat() perform. It returns 1 if file exists at * given path in any other case returns 0. */ int isFileExistsStats(const char *path) { struct stat stats; stat(path, &stats); // Examine for file existence if (stats.st_mode & F_OK) return 1; return 0; }

Within the above program I've outlined features to verify file existence utilizing all three strategies described above. Nevertheless, I have never used all of them, you should utilize any of the above outlined strategies.

Methods to verify if listing exists or not?

To verify listing existence we'll once more use stat construction. sys/stat.h header file defines a macro S_ISDIR(), used to verify listing existence. The macro accepts stat.st_mode param and returns a non-zero integer if given file is a listing, in any other case zero.

Program to verify listing existence

/** * C program to verify whether or not a listing exists or not. */ #embrace  #embrace  #embrace  int isDirectoryExists(const char *path); int essential() { char path[100]; printf("Enter listing path: "); scanf("%s", path); // Examine if listing exists or not if (isDirectoryExists(path)) { printf("Listing exists at path '%s'n", path); } else { printf("Listing doesn't exists at path '%s'n", path); } return 0; } /** * Perform to verify whether or not a listing exists or not. * It returns 1 if given path is listing and exists * in any other case returns 0. */ int isDirectoryExists(const char *path) { struct stat stats; stat(path, &stats); // Examine for file existence if (S_ISDIR(stats.st_mode)) return 1; return 0; }

Completely satisfied coding 😉

Article post on: dinhthienbao.com

Read more  How To Make a Computer Fill the TV Screen When Connecting Through HDMI

Recommended For You

About the Author: Thien Bao

Hello, my name is ThienBao. I am a freelance developer specializing in various types of code.