我是靠谱客的博主 体贴春天,这篇文章主要介绍linux下使用c判断文件夹是否为空的小程序,现在分享给大家,希望可以做个参考。

 自己写了一个 判断文件夹是否为空的小代码

//文件夹操作相关的函数的帮助
$: 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");
    }  
}

 

转载于:https://www.cnblogs.com/etangyushan/p/3714779.html

最后

以上就是体贴春天最近收集整理的关于linux下使用c判断文件夹是否为空的小程序的全部内容,更多相关linux下使用c判断文件夹是否为空内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(152)

评论列表共有 0 条评论

立即
投稿
返回
顶部