Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 首先从目录-C++;_C++ - Fatal编程技术网

C++ 首先从目录-C++;

C++ 首先从目录-C++;,c++,C++,假设我有一个目录,其中包含具有以下名称的文件: 00 01 02 03 04 05 数字越高,文件越新。我想读2个最新的文件,但按降序排列。所以我想先读取05文件,然后读取04文件 我如何才能有效地实现这一点?此目录可以有100个文件 我知道C++中有一个读写函数,但它是按升序读文件(前00个,然后01个), 非常感谢!:) 创建一个文件名数组,按降序对数组排序,按新排序数组的顺序读取文件。您可以使用boost::filesystem和std::sort对文件进行排序,然后迭代 struct

假设我有一个目录,其中包含具有以下名称的文件:

00 01 02 03 04 05

数字越高,文件越新。我想读2个最新的文件,但按降序排列。所以我想先读取05文件,然后读取04文件

我如何才能有效地实现这一点?此目录可以有100个文件

我知道C++中有一个读写函数,但它是按升序读文件(前00个,然后01个),


非常感谢!:)

创建一个文件名数组,按降序对数组排序,按新排序数组的顺序读取文件。

您可以使用
boost::filesystem
std::sort
对文件进行排序,然后迭代

struct comparator {
    bool operator()(boost::filesystem::path i, 
                    boost::filesystem::path j)
    { 
        return (i>j);
    }

} myComparator;

...

boost::filesystem::path folderPath = "/path/to/some/folder";

if (boost::filesystem::exists(folderPath))
{
    if (boost::filesystem::is_directory(folderPath))
    {
        typedef std::vector<boost::filesystem::path> vec;
        vec v;

        std::copy(
            boost::filesystem::directory_iterator(folderPath),
            boost::filesystem::directory_iterator(),
            std::back_inserter(v)
        );

        std::sort(v.begin(), v.end(), myComparator);

        for (vec::const_iterator it(v.begin()); it != v.end; ++it)
        {
            std::cout << "File: " << *it << std::endl;
        }
    }
}
结构比较器{ bool操作符()(boost::filesystem::path i, boost::文件系统::路径j) { 返回(i>j); } }霉菌比较器; ... boost::filesystem::path folderPath=“/path/to/some/folder”; 如果(boost::filesystem::exists(folderPath)) { if(boost::filesystem::is_目录(folderPath)) { typedef std::向量向量; vec-v; 复制( boost::filesystem::directory_迭代器(folderPath), boost::filesystem::directory\u iterator(), 标准:背面插入器(v) ); std::sort(v.begin()、v.end()、myComparator); for(vec::const_迭代器it(v.begin());it!=v.end;++it) {
std::cout我没有检查代码,但这应该会让您了解它的外观。阅读文件列表,对其进行排序,然后按正确的顺序打开文件

DIR dir;
struct dirent *dir_entry;
dir = opendir("dir/path")

std::vector<std::string> file_list;
file_list.reserve(N);
while(dir=readdir(dir))
{
  if (dir_entry->d_type == DT_REG)
  {
    file_list.push_back(std::string(dir_entry->d_name));
  }
}
std::sort(file_list.begin(), file_list.end());
// open files ...
...
DIR;
struct dirent*dir\u条目;
dir=opendir(“dir/path”)
std::矢量文件列表;
文件列表保留(N);
while(dir=readdir(dir))
{
如果(目录输入->数据类型==DT\U注册表)
{
文件列表。向后推(std::string(dir\u entry->d\u name));
}
}
排序(file_list.begin(),file_list.end());
//打开文件。。。
...

您可以使用
popen(“ls-t”,“r”)

文件系统不是SQL server,它不提供已排序的文件列表等。您必须遍历所有文件并查看它们的时间戳。然后按您喜欢的顺序处理它们。100秒的文件没有什么大不了的。您的瓶颈将是文件I/O。“readdir函数…以升序读取文件”-不,它不会。它以目录顺序读取文件。你不应该依赖于
readdir
返回的顺序。说真的吗?为什么不在Bash中编写整个程序呢?