List regular files only (without directory) problem

南楼画角 提交于 2019-11-29 16:30:30

It looks like the stat function is failing for some of the files, so the info struct isn't being updated and still has the data in it from "..". Replace that line with:

if (stat(file->d_name, &info))
{
    printf("error: stat(%s): %s\n", file->d_name, strerror(errno));
    continue;
}

..and you'll see why.

Yelonek

I once had to list every yml file in directory including subdirectories. It was on Win XP: system("dir C:\Path\To\File\*.yml /B /S >> list.txt"); and then I would parse list.txt to get all of the them. One line was one file. Easy, but not portable. Also that was some time ago - now I would maybe try boost.

EDIT: This answer is not about the code, but:

Actually I just would like to list the name of each file, but without directories.

If he took the same approach as me, he would have his problem solved.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!