【问题标题】:Using dirent.h but files are being skipped使用 dirent.h 但文件被跳过
【发布时间】:2014-03-14 08:31:36
【问题描述】:

我在一个名为:1.jpg3.jpg4.jpg6.jpg8.jpg10.jpg15. jpg、...100.jpg、@ 的文件夹中有数百张图片987654329@、103.jpg113.jpg 等...

我正在使用dirent.h 遍历文件,但不知何故dirent.h10.jpg 开始,它提供的下一个文件突然是100.jpg,然后是102.jpg,...为什么它会跳过一些图片?

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

cv::Mat image;

DIR *dir;
struct dirent *ent;
if ((dir = opendir ("C:\\Users\\Faraz\\Desktop\\Project\\detecting_false_positives_stuff\\face_images\\faces\\")) != NULL) {
    ent = readdir (dir);
    printf ("%s\n", ent->d_name);
    ent = readdir (dir);
    printf ("%s\n", ent->d_name);
    while ((ent = readdir (dir)) != NULL) {
        printf ("%s\n", ent->d_name);

        std::string fullPath = std::string("C:\\Users\\Faraz\\Desktop\\Project\\detecting_false_positives_stuff\\face_images\\faces\\") + ent->d_name;

        cout<<fullPath;

        image = cv::imread(fullPath);

        ...

    }
    closedir (dir);
}
return 1;

}

【问题讨论】:

    标签: c++ loops directory dirent.h


    【解决方案1】:

    如果您希望文件按顺序排列,您必须自己对文件进行排序,readdir 不会为您这样做。另见:Does readdir() guarantee an order?

    【讨论】:

    • 哦,只是顺序不同?这意味着最后我仍然会迭代 1.jpg、3.jpg 等。
    • 看起来文件是按名称按字母顺序排序的,而不是按数字排序的。因此顺序为 1.jpg、10.jpg、105.jpg、2.jpg。如果您希望它们以正确的词汇顺序排列,请在数字前加上零。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-06
    • 2018-04-01
    • 2018-03-15
    • 2020-10-09
    • 2020-05-19
    • 1970-01-01
    • 2015-08-27
    相关资源
    最近更新 更多