linux下使用c判断文件夹是否为空的小程序
时间:2014-05-08 01:31:53
收藏:0
阅读:855
/*自己写了一个 判断文件夹是否为空的小代码
打开文件夹的函数在man的时候要加 man 3 readdir, */
#include
<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
int isdirempty(char *dirname)
{
/* 打开要进行匹配的文件目录 */
DIR *dir = opendir(dirname);
struct dirent *ent;
if (dir == NULL)
{
perror("seekkey.c-98-opendir");
return -1;
}
while (1)
{
ent = readdir (dir);
if (ent <= 0)
{
break;
}
if ((strcmp(".", ent->d_name)==0) || (strcmp("..", ent->d_name)==0))
{
continue;
}
if ((ent->d_type == 4) || (ent->d_type == 8))
{
return -1;
}
}
return 0;
}
int main()
{
char *dirname = "bb";
int res = isdirempty(dirname);
if (res == 0)
{
printf ("是空\n");
}
else
{
printf ("不是空\n");
}
}
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
int isdirempty(char *dirname)
{
/* 打开要进行匹配的文件目录 */
DIR *dir = opendir(dirname);
struct dirent *ent;
if (dir == NULL)
{
perror("seekkey.c-98-opendir");
return -1;
}
while (1)
{
ent = readdir (dir);
if (ent <= 0)
{
break;
}
if ((strcmp(".", ent->d_name)==0) || (strcmp("..", ent->d_name)==0))
{
continue;
}
if ((ent->d_type == 4) || (ent->d_type == 8))
{
return -1;
}
}
return 0;
}
int main()
{
char *dirname = "bb";
int res = isdirempty(dirname);
if (res == 0)
{
printf ("是空\n");
}
else
{
printf ("不是空\n");
}
}
评论(0)