Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I check to see if a directory is empty

Status
Not open for further replies.

waynejfoster

Programmer
Aug 7, 2001
13
0
0
GB
What function can I call to see if a directory is empty?

 
struct dirent *dirp;
DIR *dp;
#include <stdio.h>
#include <dirent.h>

int no_files = 0;

if ((dp ==opendir(&quot;youdirectoryname&quot;) == (DIR *) NULL)
..... error cant open dir
else
{
while (readdir(dp)) != (DIR *) NULL)
if (strcmp(dirp->d_name, &quot;.&quot;) == 0 ||
strcmp(dirp->d_name, &quot;..&quot;) == 0)
continue;
no_files++;
}
closedir(dp);
printf(there are %d files \n&quot;, no_files);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top