Input the name of the directory (path) and then remove the given empty directory using the rmdir() function.
The rmdir() function is a library function of <sys/types.h> header file which is used to remove an empty directory, the directory must not contain any files or directories.
int rmdir( const char* path );
The argument path is the path of the directory that you want to delete.
The function returns 0 if the directory is removed successfully, it returns -1 if the directory is the current working directory of any process, If the directory is the root directory, the effect of this function depends on the filesystem.
The source code to remove an empty directory using the rmdir() function is given below. The given program is compiled and executed using GCC compile on UBUNTU 18.04 OS successfully.
// C program to remove empty directory
// using rmdir() function
int ret = 0;
printf("Enter directory name: ");
ret = rmdir(dirName);
if (ret == 0)
printf("Given empty directory removed successfully\n");
printf("Unable to remove directory %s\n", dirName);
Enter directory name: newDir
Directory created successfully
Here, we created a character array dirName. Then we read the name of the directory from the user. Then we removed the given directory using the rmdir() function and printed the appropriate message on the console screen.
total answers (1)
start bookmarking useful questions and collections and save it into your
own study-lists, login now to start creating your own collections.