linux c 下如何获得目录下的文件数目

更新时间:2023-12-06 07:49:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

linux c 下如何获得目录下的文件数目

int main(int argc, char **argv) {

DIR * pdir;

struct dirent * pdirent; struct stat f_ftime;

int fcnt;/*文件数目统计*/ pdir=opendir(\ if(pdir==NULL) { return(-1); } fcnt=0;

for(pdirent=readdir(pdir);pdirent!=NULL;pdirent=readdir(pdir)) {

if(strcmp(pdirent->d_name,\

if(stat(pdirent->d_name,&f_ftime)!=0) return -1 ;

if(S_ISDIR(f_ftime.st_mode)) continue; /*子目录跳过*/ fcnt++;

printf(\文件:%s\\n\ }

printf(\文件总数%d\\n\ closedir(pdir); return 0; }

#include #include #include #include #include

void printdir(char *dir, int depth) {

DIR *dp;

struct dirent *entry; struct stat statbuf;

if((dp = opendir(dir)) == NULL) {

fprintf(stderr, \ return; }

chdir(dir);

while((entry = readdir(dp)) != NULL) { lstat(entry-> d_name,&statbuf); if(S_ISDIR(statbuf.st_mode)) {

/**//* Found a directory, but ignore . and .. */ if(strcmp( \ strcmp( \ continue;

printf( \ /**//* Recurse at a new indent level */ printdir(entry-> d_name,depth+4); }

else printf( \ }

chdir( \ closedir(dp); }

/**//* Now we move onto the main function. */

int main(int argc, char* argv[]) {

char *topdir, pwd[2]= \ if (argc != 2) topdir=pwd; else

topdir=argv[1];

printf( \ printdir(topdir,0); printf( \

exit(0); }

本文来源:https://www.bwwdw.com/article/6prt.html

Top