2014-02-14 62 views
1

我有一个文件夹名为几百图片:1.jpg3.jpg4.jpg6.jpg8.jpg10.jpg15. jpg,... 100.jpg102.jpg103.jpg113.jpg等。 。使用dirent.h但文件被忽略

我使用dirent.h通过文件迭代,但不知何故dirent.h开始在10.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; 

}

回答

1

你必须,如果你想他们为了给自己的文件排序,readdir不会为你做的。另请参阅:Does readdir() guarantee an order?

+0

噢,所以只是顺序是不同的?这意味着最后我仍然会遍历1.jpg,3.jpg等。 – farahm

+0

是的,它最终应该达到1.jpg等等。 – Unknown

+0

看起来像按字母顺序*按名称排序文件,而不是按数字排序。因此命令1.jpg,10.jpg,105.jpg,2.jpg。如果你想让它们按照正确的词法顺序排列,则用零作为数字前缀。 –