24

I'm using opendir function to check if a directory exists. The problem is that I'm using it on a massive loop and it's inflating the ram used by my app.

What is the best (fastest) way to check if a directory exists in C? What is the best (fastest) way to create it if doesn't exists?

2
  • 8
    Silly question, are you calling closedir() afterwards?
    – user7116
    Feb 16, 2012 at 16:04
  • And the faster way might be to do it asynchronously, but you should consider the capabilities of your media. If it is regular HDD then it might fail into seek-storm and this will work slow. But if you use RAID/SSD or even working in the cloud, this will work.
    – LiMar
    Feb 16, 2012 at 16:09

5 Answers 5

50

Consider using stat. S_ISDIR(s.st_mode) will tell you if it's a directory.

Sample:

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

...
struct stat s;
int err = stat("/path/to/possible_dir", &s);
if(-1 == err) {
    if(ENOENT == errno) {
        /* does not exist */
    } else {
        perror("stat");
        exit(1);
    }
} else {
    if(S_ISDIR(s.st_mode)) {
        /* it's a dir */
    } else {
        /* exists but is no dir */
    }
}
...
5
  • 1
    how faster does it is comparing with opendir? Feb 16, 2012 at 16:22
  • 3
    That depends on the operating system and the number of entries there are in the parent directory. It will be faster in any way and that's what stat is made for. Feb 16, 2012 at 16:24
  • Would lstat also be an option?
    – cwd
    Jun 8, 2013 at 22:09
  • @cwd , yes, I think lstat would possibly be the even better option because it does not follow symlinks. Depends on what is wanted though... Jun 8, 2013 at 22:32
  • I am using this to test for "D:\path\subpath" and I've created the directory. It is telling me the dir does not exist. :/ (using Windows/MinGW)
    – IAbstract
    Mar 30, 2015 at 13:22
25

You could call mkdir(). If the directory does not exist then it will be created and 0 will be returned. If the directory exists then -1 will be returned and errno will be set to EEXIST.

6
  • 2
    With the caveat that ENOENT may occur if something along the path doesn't exist as well.
    – user7116
    Feb 16, 2012 at 16:07
  • True. Should have mentioned that.
    – ckruse
    Feb 16, 2012 at 16:08
  • Since you want to create the directory if it doesn't exist: yes. You save a stat() call.
    – ckruse
    Feb 16, 2012 at 20:53
  • 1
    Note that you need to include <sys/stat.h> (for mkdir) and <errno.h> (to handle errno and EEXIST). Jul 21, 2013 at 18:09
  • 1
    Note however that if you DON'T WANT to create the directory (but only check for existence, per first half of the question), then then mkdir costs you I/O (might be synchronized, costing an even greater hit in I/O performance for other actors on the system) and might fail for reasons unrelated to existence such as current account's permissions, mount options (R/O FS), or available filesystem space or quota.
    – Jim Klimov
    Jul 21, 2020 at 9:15
21

I prefer using access()

if (0 != access("/path/to/possible_dir/", F_OK)) {
  if (ENOENT == errno) {
     // does not exist
  }
  if (ENOTDIR == errno) {
     // not a directory
  }
}

If you ensure a trailing / in the directory name, this works perfectly.

2
3

I would use stat(), if available.

0

It sounds like you have a memory leak. Calling opendir should not inflate the RAM of your app as long as you remember to always call closedir after successfully opening a directory. Also, make sure you are freeing any buffers you allocated to compute the directory name.

1
  • Yeap I'm closing it, I'll just use stat, thanks for your time! Feb 16, 2012 at 16:28

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.