-1

How can I find if a directory exists or not in a C program? I know that getcwd() gives you the current directory but I want to find ANY directory. Is there a function for that or how do I do it? I am using Ubuntu

Grijesh Chauhan
  • 55,177
  • 19
  • 133
  • 197

3 Answers3

1

opendir, readdir and closedir are POSIX functions, so they should be available in Linux, MacOS, Windows as well as any Unix type system.

Klas Lindbäck
  • 32,669
  • 4
  • 56
  • 80
0

you can use access() function , like for example:

access(path, F_OK);

It returns 0 if found. -1 if not found.

Denny Mathew
  • 892
  • 8
  • 13
0
int mkdir (const char *filename, mode_t mode)

you need to include the header file sys/stat.h to use this function.

The mkdir function creates a new, empty directory with name filename. The argument mode specifies the file permissions for the new directory file.A return value of 0 indicates successful completion, and -1 indicates failure.

In case of failure and if your Directory already exists errno value will equal to EEXIST.

Dayal rai
  • 6,308
  • 21
  • 29