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

#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);
}

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命令

试一下这个函数,或者参考这篇博客:https://blog.csdn.net/L_J_Kin/article/details/108536639

void getFiles(string path, vector <string> & files)
{
    long hFile = 0;
    struct _finddata_t fileinfo;
    string pathp;
    if ((hFile = _findfirst(pathp.assign(path).append("\\*").c_str(), &fileinfo)) != -1)
    {
        do
        {
            if ((fileinfo.attrib &  _A_SUBDIR))
            {
                if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
                {                    
                    continue;
                }
            }
            else
            {
                string filestr = fileinfo.name;
                files.push_back(filestr);
            }
        } while (_findnext(hFile, &fileinfo) == 0);
        _findclose(hFile);
    }

}
#include "apue.h"
#include <dirent.h>
#define MAX 30

int main(int argc, char argv[]) {
        if(argc!=2)
        loop(argv[0], 0);
        exit(0);
}



void dopath(char* pathname) {
        struct stat statbuf;
        struct dirent* dirp;
        DIR* dp;
        if (lstat(pathname, &statbuf) < 0) {
                err_sys("lstat error");
        }
        if ((dp = opendir(pathname)) == NULL)
                err_sys("can't opendir");
        while ((dirp = readdir(dp)) != NULL) {
                if (strcmp(dirp->d_name, ".") == 0 ||
                        strcmp(dirp->d_name, "..") == 0)
                        continue;
                printf("%s%s/\n",pathname,dirp->d_name);
        }
        chdir("dirp->d_name");
        closedir(dp);
        return;
}

void loop(char* n, int x) {
        char* path;
        for (x = 0; x < MAX; x++) {
                dopath(path);
                printf("%s", n);
                return;
        }
}


这个代码,不知道是哪里出了问题,没有效果啊!