Puedes usar opendir()
y comprueba si ENOENT == errno
en caso de falla:
#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. */
}
Utilice el siguiente código para comprobar si existe una carpeta. Funciona en plataformas Windows y Linux.
#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");
}
}
Podrías usar stat()
y pasarle la dirección de un struct stat
, luego verifique su miembro st_mode
por tener S_IFDIR
establecer.
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
...
char d[] = "mydir";
struct stat s = {0};
if (!stat(d, &s))
printf("'%s' is %sa directory.\n", d, (s.st_mode & S_IFDIR) : "" ? "not ");
// (s.st_mode & S_IFDIR) can be replaced with S_ISDIR(s.st_mode)
else
perror("stat()");