10

我正在使用Boost::FileSystem在 Linux 平台下运行 C++ 的库,我有以下问题:

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

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

如果是,您能否提供示例代码?

4

2 回答 2

15

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 ;
   }
}
于 2010-11-25T16:47:14.197 回答
1

您可以使用 std::map(last_write_time, fileName) 来存储文件上次修改时间和绝对文件路径,并按顺序遍历对数据进行排序。

于 2013-11-10T18:00:17.413 回答