2010-11-25 29 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,文件名)來存儲最後修改時間的文件和文件絕對路徑和做一箇中序遍歷排序數據。