2016-07-02 2 views
0

まずは、Boost :: Filesystemに基づいたC++ 17標準のFilesystemライブラリのVisual Studio 2015の実装を使用しています。新しく作成されたファイルの「最後の書き込み時間」を変更できないのはなぜですか?

基本的には、ファイルのタイムスタンプ(「最後の書き込み時間」)を保存し、そのファイルの内容を前記のタイムスタンプとともにアーカイブにコピーし、そのファイルを取り出して保存します正しい "最後の書き込み時間"を復元します。

// Get the file's 'last write time' and convert it into a usable integer. 
__int64 timestamp = chrono::time_point_cast<chrono::seconds>(fs::last_write_time(src)).time_since_epoch().count(); 

// ... (do a bunch of stuff in here) 

// Save the file 
ofstream destfile(dest, ios::binary | ios::trunc); 
destfile.write(ptr, size); 

// Correct the file's 'last write time' 
fs::last_write_time(dest, chrono::time_point<chrono::system_clock>(chrono::seconds(timestamp))); 

問題は、私は全くlast_write_time()と呼ばれることはありませんように、新しいファイルは常に、それは(今のところ)に作成された時間に等しいタイムスタンプで終わるだろうということです。

タイムスタンプを既存のファイルから別のファイルにコピーしようとすると、正常に動作します。ファイルからタイムスタンプをコピーするときには、fs::copyを使用してそのファイルの新しいコピーを作成し、すぐにコピーのタイムスタンプを変更します。次のコードは正しく動作:

// Get the file's 'last write time' and convert it into a usable integer. 
__int64 timestamp = chrono::time_point_cast<chrono::seconds>(fs::last_write_time("test.txt")).time_since_epoch().count(); 
fs::copy("test.txt", "new.txt"); 
// Correct the file's 'last write time' 
fs::last_write_time("new.txt", chrono::time_point<chrono::system_clock>(chrono::seconds(timestamp))); 

私は、タイムスタンプを格納することは正しくない可能性があることを疑う理由はないが、私は他のアイデアを持っていません。これは何が原因でしょうか?

答えて

4

これはストリームに書き込みましたが、実際に時刻を更新する前にファイルを閉じなかったためです。終了時にもう一度更新されます。

解決方法は、ストリームを閉じてファイルの時刻を更新することです。

+0

これは、ありがとう! –

+0

問題なし:)(コメントを長くする) –

関連する問題