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
Asked
Active
Viewed 1,194 times
-1

Grijesh Chauhan
- 57,103
- 20
- 141
- 208

Monster Monster
- 9
- 1
- 6
-
2What OS are you using?? Filesystem-API is OS-dependent – bash.d May 22 '13 at 08:25
-
1This might be of help? http://stackoverflow.com/questions/9314586/c-faster-way-to-check-if-a-directory-exists – Kevin Brydon May 22 '13 at 08:26
-
`man opendir`, `man stat` (of course, if you are using UNIX-based OS). – Eddy_Em May 22 '13 at 08:29
-
Why do you not do some research before posting? – Anish Ramaswamy May 22 '13 at 08:47
3 Answers
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
- 33,105
- 5
- 57
- 82
0
you can use access() function , like for example:
access(path, F_OK);
It returns 0 if found. -1 if not found.

Denny Mathew
- 1,072
- 9
- 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,548
- 22
- 29