C ++以升序打印文件名 [英] C++ printing out file names in ascending order
问题描述
我使用以下代码来遍历目录中的文件。
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
对数组进行排序。
When you've read all of them, use std::sort
to sort the array.
然后按照您的要求处理排序后的文件名。
Then process the sorted file names as you see fit.
这篇关于C ++以升序打印文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!