linux用C如何判断一个目录是否为空

欢迎进入C/C++编程社区论坛,与200万技术人员互动交流 >>进入

用opendir打开一个目录,得到结构DIR*,但是没有关于其下有多少文件、子目录的数据,我用比较土的办法,遍历目录readdir,计算其下有多少文件和子目录,当然也不是连子目录下的东西也找。 下面的程序在solaris8、gcc编译通过的,如果一个目录是空的,输出为2。

#include #include #include int main(int argc , char **argv) { DIR *dirp; int num=0; dirp = opendir(argv[1]); while (dirp) { if ( readdir(dirp) != NULL) ++num; else break; } closedir(dirp); printf(“%d\n”,num); }

shell中判断目录为空

#!/bin/ksh# Check if a directory is empty or notif [ $# = 0 ]thenechoecho “use this tool to check if directory is empty”echo “for example: isEmpty dirName “echoexit 1ficase $(( 0 + $(find $1 2>&- |head -2|wc -l))) in0) echo Permission denied ! ;;1) echo Directory is empty ! ;;*) echo Directory is not empty ! ;;esac

如若今生再相见,哪怕流离百世,迷途千年,也愿。

linux用C如何判断一个目录是否为空

相关文章:

你感兴趣的文章:

标签云: