名稱空間
變體
操作

std::experimental::filesystem::last_write_time

來自 cppreference.com
< cpp‎ | experimental‎ | fs
 
 
實驗性
技術規範
檔案系統庫 (檔案系統 TS)
庫基礎 (庫基礎 TS)
庫基礎 2 (庫基礎 TS v2)
庫基礎 3 (庫基礎 TS v3)
並行性擴充套件 (並行性 TS)
並行性擴充套件 2 (並行性 TS v2)
併發性擴充套件 (併發性 TS)
併發擴充套件 2 (併發 TS v2)
概念 (概念 TS)
範圍 (範圍 TS)
反射 (反射 TS)
數學特殊函式 (特殊函式 TR)
實驗性非 TS
模式匹配
線性代數
std::execution
契約
2D 圖形
 
 
定義於標頭檔案 <experimental/filesystem>
file_time_type last_write_time( const path& p );
file_time_type last_write_time( const path& p, error_code& ec )
(1) (filesystem TS)
void last_write_time( const path& p, file_time_type new_time );
void last_write_time( const path& p, file_time_type new_time, error_code& ec );
(2) (filesystem TS)
1) 返回 p 的最後修改時間,其確定方式如同訪問 POSIX stat 的成員 st_mtime(符號連結被跟蹤)。不丟擲異常的過載在錯誤時返回 file_time_type::min()
2) 更改 p 的最後修改時間,如同透過 POSIX futimens(符號連結被跟蹤)。

目錄

[編輯] 引數

p - 要檢查或修改的路徑
new_time - 新的修改時間
ec - 非丟擲過載中用於錯誤報告的出參

[編輯] 返回值

1) p 的最後修改時間。
2) (無)

[編輯] 異常

不帶 error_code& 引數的過載在底層 OS API 錯誤時丟擲 filesystem_error,構造時以 p 作為第一個引數,OS 錯誤碼作為錯誤碼引數。如果記憶體分配失敗,可能會丟擲 std::bad_alloc。帶 error_code& 引數的過載在 OS API 呼叫失敗時將其設定為 OS API 錯誤碼,如果沒有錯誤發生則執行 ec.clear()。此過載具有
noexcept 規範:  
noexcept
  

[編輯] 注意

不保證在設定寫入時間後,(1) 返回的值與傳遞給 (2) 的引數相同,因為檔案系統的時間可能比 file_time_type 更精細。

[編輯] 示例

#include <chrono>
#include <experimental/filesystem>
#include <fstream>
#include <iomanip>
#include <iostream>
namespace fs = std::experimental::filesystem;
using namespace std::chrono_literals;
 
int main()
{
    fs::path p = fs::current_path() / "example.bin";
    std::ofstream(p.c_str()).put('a'); // create file
    auto ftime = fs::last_write_time(p);
 
    std::time_t cftime = decltype(ftime)::clock::to_time_t(ftime); // assuming system_clock
    std::cout << "File write time is " << std::asctime(std::localtime(&cftime)) << '\n';
 
    fs::last_write_time(p, ftime + 1h); // move file write time 1 hour to the future
    ftime = fs::last_write_time(p); // read back from the filesystem
 
    cftime = decltype(ftime)::clock::to_time_t(ftime);
    std::cout << "File write time is " << std::asctime(std::localtime(&cftime)) << '\n';
    fs::remove(p);
}

可能的輸出

File write time is Tue Mar 31 19:47:04 2015
 
File write time is Tue Mar 31 20:47:04 2015

[編輯] 另見

表示檔案時間值
(typedef) [編輯]