C ++:ファイルシステムをブーストして、特定の時間より古いファイルのリストを返す
-
28-09-2019 - |
質問
私はを使用しています Boost::FileSystem
Linuxプラットフォームで実行されているC ++を備えたライブラリと、次の質問があります。
特定の日付時刻よりも古く変更されたファイルのリストが欲しいです。私はかどうかわかりません boost::FileSystem
次のような方法を提供します:
vector<string> listFiles = boost::FileSystem::getFiles("\directory", "01/01/2010 12:00:00");
はいの場合、サンプルコードを提供していただけますか?
解決
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 ;
}
}
他のヒント
std :: map(last_write_time、filename)を使用して、最後の変更時刻と絶対ファイルパスを保存し、データをソートするための順序トラバーサルを実行できます。
所属していません StackOverflow