How to in C Check if Directory Exists or Not

Often I find myself feeling frustrated just trying to do basic things in C that I can take for granted in Java or Python. And, running C code to check if directory exists or not in the file system in Linux, and the knowing the best way check if directory exists is one of the those situations.

Obviously, I’m a competent C programmer, but it’s these little tedious things in C that make me feel like I’m just starting out sometimes.

With that as motivation, I’m putting my solution in this post for all the world to see. Maybe you’ve a better way to do it? If so, let me know in the comments below.

Solving C Check if Directory Exists

My approach was first to go and check a dusty C/C++ book I had laying about called Jamsa’s C/C++ bible.

A worthy book I should say too, if you like super technical and optimal solutions to C problems e.g., C check if a directory exists.

That has some solutions, but I was still curious about other solutions and people’s answers. So the next step was a search through google searching for C check if a directory exists.

By this point, I had noted a number of solutions to this. Basically, you need to call the C fopen() function to check for a file in a directory existing or not.

How to Use fopen() to Check Files

Unlike in other languages C does not use methods or classes. The equivalent to a method is a function.

Here, we need to use fopen() to check if a file is in a directory or not. And, like most C functions for IO it returns a pointer to a FILE structure if it was called successfully, or Null otherwise.

So, what does that mean or do to help? Well, we can just call fopen() and inspect its return value. Slick!

When the function call returns a FILE object this indicates the file exists, and if we get a Null back this means it doesn’t. It comes down to a binary value really.

But, as with most things in C programming thre is a downside and opportunities for introducing bugs into your code.

A file may actually exist, however if the user you’re logged in as doesn’t have the right file access permissions it will not return a FILE pointer. It will return Null instead.

Checking for File Access in C

So, the next part of the puzzle is to check for access permissions.

We can do this using the C access() function, which returns an int indicating if access is possible to the file passed in as a parameter.

To use access(), we need to pass in two values: const char *path and int amode.

These parameters specify the directory to check for access to and how.

The amode parameter is basically a bit pattern created by using a combination of the F_OK, R_OK, W_OK and X_OK constants, or using one as a standalone constant.

Now, when we call this function it will return -1 if access is not granted and 0 if it is.

Final C code to Check if Directory Exists

Now the final C solution to this is as follows.

Step 1: The first step is to implement a function with fopen() to check for a file as follows.

int checkFile( const char *thePath)
 {  
   FILE *filePointer = fopen(thePath, "r");
   if ( filePointer == NULL) {     
     return 0;  
   }
   else {
     fclose( filePointer ); 
     return 1;
   }
 }

Step 2: Now the next thing we need to do is implement a function using access() as follows.

int fileAccess(const char *thePath)
 {
   if ( access(thePath, F_OK) == -1) 
   {
     return 0;
   }
   else
   { 
     return 1;
   }
}

Step 3: Next, we just call these two utility functions in some test code as follows, I’ve left off all the boilerplate code.

if ( fileAccess("/home/user/myfile.txt") == 1 )
 {
   if (  checkFile("/home/user/myfile.txt") == 1 )
   {
      printf( "Directory exists\n" );
   }
   else
   {
      printf( "Directory doesn't exist\n" );
   }
 }
 else
 {
   printf( "User does not have access\n" );
 }

Summary

So there it is, that’s the solution I came up to check if directory exists or not with after all the searching and reading. I hope you find it help, leave any thoughts in the comments below.