tik2kk 2022-04-04 02:20 采纳率: 50%
浏览 27

问一下各位,这个文件目录遍历,思路没错,但是总是达不到效果,为啥?

#include <dirent.h>
#include "apue.h"

static int loop(const char* pathname);

int main(int argc, char* argv[])
{
struct stat buf;
if (lstat(argv[1], &buf) < 0) {
err_ret("lstat error");
continue;
}
else
loop(argv[1]);
printf("%s", argv[1]);
exit(0);
}

static int loop(const char* pathname)
{
struct dirent* dirp;
DIR* dp;
if ((dp = opendir(pathname)) == NULL)
err_sys("can't opendir %s", pathname);
if ((dirp = readdir(dp)) != NULL) {
if (strcmp(dirp->d_name, ".") == 0 || strcmp(dirp->d_name, "..") == 0)
continue;
loop(dirp);
strcpy(pathname, dirp->d_name);
printf("%s", dirp->d_name);
}
if (closedir(dp) < 0)
err_ret("can't close directory %s", pathname);
return(0);
}

  • 写回答

3条回答 默认 最新

  • 赵4老师 2022-04-04 08:01
    关注

    system("dir /b /a-d c:\. >d:\allfiles.txt");
    //读文件d:\allfiles.txt的内容即C:\下所有文件的名字
    system("dir /b /a-d /s c:\. >d:\allfilesinsub.txt");
    //读文件d:\allfilesinsub.txt的内容即C:\下所有文件的名字包含子目录
    system("dir /b /ad c:\. >d:\alldirs.txt");
    //读文件d:\alldirs.txt的内容即C:\下所有子目录的名字
    请记住,能用shell命令获取文件、文件夹信息或者操作文件、文件夹最好用shell命令获取或者操作,而不要用各种API获取或者操作,因为当遇到非法文件夹名或非法文件名或非法文件长度、非法文件日期、压缩文件、链接文件、稀疏文件……等各种意料之外的情况时,API会处理的不全面或陷入死循环,而shell命令不会。
    如果嫌system黑窗口一闪,将system("...")替换为WinExec("cmd /c ...",SW_HIDE);

    linux下使用ls命令

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 4月4日

悬赏问题

  • ¥15 matlab有关债券凸性久期的代码
  • ¥15 lvgl v8.2定时器提前到来
  • ¥15 qtcp 发送数据时偶尔会遇到发送数据失败?用的MSVC编译器(标签-qt|关键词-tcp)
  • ¥15 cam_lidar_calibration报错
  • ¥15 拓扑学,凸集,紧集。。
  • ¥15 如何扩大AIS数据容量
  • ¥15 单纯型python实现编译报错
  • ¥15 c++2013读写oracle
  • ¥15 c++ gmssl sm2验签demo
  • ¥15 关于模的完全剩余系(关键词-数学方法)