我正在使用以下代码遍历目录中的文件。 该目录中的所有文件都有编号,每个文件都有唯一的编号。
下面的代码成功打印出目录中的所有文件,但不是按升序排列。 它完全随机出现,一个文件被复制。
我无法找到另一种打印文件或按升序打印文件的方式。 我真的需要按照升序打印它们,然后按升序打开它们。 任何帮助表示赞赏。
DIR *dpdf; struct dirent *epdf;
dpdf = opendir("/data/files"); if (dpdf != NULL){ while (epdf = readdir(dpdf)){ printf("%s\n",epdf->d_name); // std::out << epdf->d_name << std::endl; } }I am using the following code to iterate through files in the directory. All the files in this directory are numbered, each with a unique number.
The code below successfully prints out all the files in the directory however not in ascending order. It comes out completely random and one file is duplicated.
I can't find another way of printing out the files or printing them in ascending order. I really need to print them in ascending order and then later open them in ascending order. Any help is appreciated.
DIR *dpdf; struct dirent *epdf;
dpdf = opendir("/data/files"); if (dpdf != NULL){ while (epdf = readdir(dpdf)){ printf("%s\n",epdf->d_name); // std::out << epdf->d_name << std::endl; } }最满意答案
在读取文件时,将文件名放入矢量中。
当你读完所有这些内容时,使用std::sort对数组进行排序。
然后按照您认为合适的方式处理排序的文件名。
Put the file names into a vector as you read them.
When you've read all of them, use std::sort to sort the array.
Then process the sorted file names as you see fit.
更多推荐
发布评论