Directory Related Calls in C Language
Here are the summary of the system calls to manipulate the directories.
Function | Description | Returns |
mkdir(const char * pathname, mode) | Create a dirctory | 0 if OK, -1 on error |
rmdir(const char * pathname) | Delete a directory | 0 if OK, -1 on error |
opendir(const char * pathname) | Open a directory | Pointer of |
readdir(DIR * dp) | Read a directory | Pointer of |
closedir(DIR * dp) | Close a directory | 0 if OK, -1 on error |
In *nix, we have a special data structure for a directory, namelyDIR
anddirent
.
DIR
is a structure for directory streams, anddirent
has the following members:
ino_t d_ino
: file serial number
char d_name[]
: Name of entry.
Example
In the following, we try to make use ofopendir
andreaddir
to traverse the directory, simulating a simplels
.
When you run the command `./listdir .',
The content of the current directory is printed.
Last updated