C ++按升序打印出文件名(C++ printing out file names in ascending order)

编程入门 行业动态 更新时间:2024-10-26 14:38:43
C ++按升序打印出文件名(C++ printing out file names in ascending order)

我正在使用以下代码遍历目录中的文件。 该目录中的所有文件都有编号,每个文件都有唯一的编号。

下面的代码成功打印出目录中的所有文件,但不是按升序排列。 它完全随机出现,一个文件被复制。

我无法找到另一种打印文件或按升序打印文件的方式。 我真的需要按照升序打印它们,然后按升序打开它们。 任何帮助表示赞赏。

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.

更多推荐

本文发布于:2023-07-26 08:11:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1272902.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:升序   文件名   printing   file   ascending

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!