2010-11-25 63 views
7

我使用Boost::FileSystem库与C++在Linux平台上运行,我有以下问题:C++:升压文件系统返回于特定时间,旧文件的列表

我想有一个列表修改比给定日期时间更早的文件。我不知道是否boost::FileSystem提供这样的方法:

vector<string> listFiles = boost::FileSystem::getFiles("\directory", "01/01/2010 12:00:00"); 

如果是,请您提供的示例代码?

+0

可能重复的[我怎样才能得到一个文件夹中的文件,其中的文件按修改日期时间排序](http://stackoverflow.com/questions/4283546/how-can-i-get-一个文件夹在哪个文件夹中的文件是按照mod排序的) – dandan78 2015-03-18 14:36:28

回答

11

Boost :: filesystem不提供完全一样的功能。但是你可以使用这个:

http://www.boost.org/doc/libs/1_45_0/libs/filesystem/v3/doc/reference.html#last_write_time

为基础编写自己的。下面是一些示例代码中使用last_write_time:

#include <boost/filesystem/operations.hpp> 
#include <ctime> 
#include <iostream> 

int main(int argc , char *argv[ ]) { 
    if (argc != 2) { 
     std::cerr << "Error! Syntax: moditime <filename>!\n" ; 
     return 1 ; 
    } 
    boost::filesystem::path p(argv[ 1 ]) ; 
    if (boost::filesystem::exists(p)) { 
     std::time_t t = boost::filesystem::last_write_time(p) ; 
     std::cout << "On " << std::ctime(&t) << " the file " << argv[ 1 ] 
    << " was modified the last time!\n" ; 
     std::cout << "Setting the modification time to now:\n" ; 
     std::time_t n = std::time(0) ; 
     boost::filesystem::last_write_time(p , n) ; 
     t = boost::filesystem::last_write_time(p) ; 
     std::cout << "Now the modification time is " << std::ctime(&t) << std::endl ; 
     return 0 ; 
    } else { 
     std::cout << "Could not find file " << argv[ 1 ] << '\n' ; 
     return 2 ; 
    } 
} 
+0

谢谢。我从Boost:fileSystem中看到了这个示例代码,但是如何对这些文件进行排序? – olidev 2010-11-26 08:12:51

1

您可以使用一个std ::地图(last_write_time,文件名)来存储最后修改时间的文件和文件绝对路径和做一个中序遍历排序数据。

相关问题