devxlogo

Facilitate Directory Operations with the <dirent.h> and <dir.h> Libraries

Facilitate Directory Operations with the <dirent.h> and <dir.h> Libraries

isk defragmenters, antiviruses, backup utilities, and file compression tools are a few examples of applications that operate on directories. Alas, standard C++ doesn’t have a library for manipulating directories. Consequently, programmers resort to third-party libraries and platform-dependent hacks for mundane operations such as listing files in a directory, creating new directories, and deleting directory files. These workarounds compromise code portability and force developers to learn different libraries every time. Thankfully, you can use the quasi-standard and libraries to manipulate directories in a portable and platform-neutral fashion.


How can you operate on directories using a portable and standardized library?


Use the and libraries to list directory files and perform other directory-related operations.

Reading a Directory’s Contents
Before you can view the files in a directory you need to open it. declares the following functions for opening, reading, closing, and rewinding a directory.

To open a directory, use opendir():

DIR * opendir(const char * pathname);

This function returns a pointer to a DIR data structure that represents a directory. A NULL value indicates an error. pathname must be a name of an existing directory.

After opening a directory, use readdir() to traverse it:

struct dirent * readdir (DIR * pdir);

pdir is the result of a previous opendir() call. readdir() returns a pointer to a dirent structure whose member d_name contains the name of the current file (the rest of dirent’s members depend on the specific file system installed on your computer). Each successive call advances to the next file in the directory. There is one quirk here, though. readdir() returns NULL under two conditions: if an error occurs or once you have traversed all the files in the directory. To distinguish between these two cases, be sure to examine errno after every readdir() call. Remember that readdir() doesn’t change errno unless an error has occurred. Therefore, reset errno explicitly before calling this function.

Figure 1. Typical Output: This is an example of the typical output of the program.

After viewing the contents of directory you need to close it explicitly by calling closedir():

int closedir (DIR * pdir);

pdir is the result of a previous opendir() call. Here is a complete program that lists the contents of the current directory:

#include  #include  #include  #include  int main() { DIR *pdir; struct dirent *pent; pdir=opendir("."); //"." refers to the current dir if (!pdir){  printf ("opendir() failure; terminating");  exit(1); } errno=0;  while ((pent=readdir(pdir))){   printf("%s", pent->d_name); } if (errno){  printf ("readdir() failure; terminating");  exit(1); } closedir(pdir);}

Figure 1 shows a typical output from this program.

Creating, Deleting, and Changing a Directory
Some of the directory-related operations such as creating, deleting, and changing a directory are grouped in another quasi-standard library called . In POSIX systems, these functions are declared in .

To create a new directory, use mkdir():

int mkdir(const char * dirname, [mode_t perm]);

dirname is the name of the directory to be created. If it’s an existing file name or an invalid directory string, mkdir() fails. The perm argument is used in POSIX systems to specify the directory’s permissions. It doesn’t exist in the Windows version of this function.

To delete an existing directory, use rmdir():

int rmdir(const char * dirname);

The directory must be empty. Otherwise, rmdir() fails.

The getcwd() function retrieves the full path name (including the drive) of the current working directory:

char *getcwd(char *path, int num);

This function is tricky and requires special attention. The path argument points to an array of num characters. If the full pathname length (including the terminating ‘

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist