How can I check if a directory exists?
You can use opendir()
and check if ENOENT == errno
on failure:
#include <dirent.h>
#include <errno.h>
DIR* dir = opendir("mydir");
if (dir) {
/* Directory exists. */
closedir(dir);
} else if (ENOENT == errno) {
/* Directory does not exist. */
} else {
/* opendir() failed for some other reason. */
}
Use the following code to check if a folder exists. It works on both Windows & Linux platforms.
#include <stdio.h>
#include <sys/stat.h>
int main(int argc, char* argv[])
{
const char* folder;
//folder = "C:\\Users\\SaMaN\\Desktop\\Ppln";
folder = "/tmp";
struct stat sb;
if (stat(folder, &sb) == 0 && S_ISDIR(sb.st_mode)) {
printf("YES\n");
} else {
printf("NO\n");
}
}