69,382
社区成员
发帖
与我相关
我的任务
分享
void recu_list(const char *path, const FILE *fout) {
if(fout==NULL){
exit(-1);
}
//indent为输出时的缩进
int indent = 2;
struct dirent* ent = NULL;
DIR *pDir;
char dir[512];
struct stat statbuf;
if ((pDir = opendir(path)) == NULL) {
fprintf(stderr, "Cannot open directory:%s\n", path);
return;
}
while ((ent = readdir(pDir)) != NULL) {
//得到读取文件的绝对路径名
snprintf(dir, 512, "%s/%s", path, ent->d_name);
//得到文件信息
lstat(dir, &statbuf);
//判断是目录还是文件
if (S_ISDIR(statbuf.st_mode)) {
//排除当前目录和上级目录
if (strcmp(".", ent->d_name) == 0 || strcmp("..", ent->d_name) == 0)
continue;
//如果是子目录,递归调用函数本身,实现子目录中文件遍历
printf("%*s子目录:%s/\n", indent, "", ent->d_name);
//递归调用,遍历子目录中文件
recu_list(dir, fout);
} else {
printf("%*s文件:%s\n", indent, "", ent->d_name);
fprintf(fout, "%s %s\n", ent->d_name);
}
}//while
closedir(pDir);
}
void recu_list(const char *path, const FILE *fout)
{
if(fout==NULL)
{
exit(-1);
}
//indent为输出时的缩进
int indent = 2;
struct dirent* ent = NULL;
DIR *pDir;
char dir[512];
struct stat statbuf;
if ((pDir = opendir(path)) == NULL)
{
fprintf(stderr, "Cannot open directory:%s\n", path);
return;
}
while ((ent = readdir(pDir)) != NULL)
{
//得到读取文件的绝对路径名
snprintf(dir, 512, "%s/%s", path, ent->d_name);
//得到文件信息
lstat(dir, &statbuf);
//判断是目录还是文件
if (S_ISDIR(statbuf.st_mode))
{
//排除当前目录和上级目录
if (strcmp(".", ent->d_name) == 0 || strcmp("..", ent->d_name) == 0)
continue;
//如果是子目录,递归调用函数本身,实现子目录中文件遍历
printf("%*s子目录:%s/\n", indent, "", ent->d_name);
//递归调用,遍历子目录中文件
recu_list(dir, fout);
}
else
{
printf("%*s文件:%s\n", indent, "", ent->d_name);
fprintf((FILE *)fout, "%s \n", ent->d_name);
}
}//while
closedir(pDir);
}