Linux獲取檔案資訊

2021-09-08 10:29:25 字數 760 閱讀 1940

專案中需要對檔案進行處理並分析,首先需要根據要求找到該檔案,比如最後修改的檔案

**實現:

1 #include 2 #include 3 #include 4

5 #include 6

7using

namespace

std;89

int main(int argc, char **ar**)

10

使用到的函式有:

stat,定義在sys/stat.h標頭檔案中,用於根據檔名獲取檔案的資訊

ctime定義在time.h標頭檔案中,用於將時間time_t型別的時間轉換為char*型別的時間

結構體有:struct stat

stat函式執行後,正確則返回0,否則返回-1,錯誤**儲存在errno中,如果執行成功,則將指定檔案的資訊填充到第二個引數指向的結構體中。

struct stat的定義如下:

1

struct

stat

2;

其中,我們使用到的成員變數為st_mtime。得到該該變數,即可根據變數的值來確定哪個檔案時最後被修改的,從而得到最新的檔案。

st_mtime是time_t型別,輸出就是一串數字,如果想要直觀的檢視時間,可以用ctime函式對其進行轉換後輸出,轉換前後輸出對比:

1 轉換前:1414482116

2 轉換後:tue oct 28

15:43:46

2014

linux 獲取檔案系統資訊(磁碟資訊)

源 例如以下 include include include include 檔案系統資訊結構體 struct filesystem info 獲取檔案系統資訊 int get filesystem info const char filesystem name,struct filesystem ...

獲取檔案資訊

include struct stat 函式宣告 int stat const char path,struct stat buffer 注意 win32 stat linux stat 引數 path 檔案或者資料夾的路徑 buffer 獲取的資訊儲存在記憶體中 返回值 正確 返回0 錯誤 返回 ...

獲取檔案版本資訊

ref copy char szfilename c windows system32 cmd.exe dword dwsize getfileversioninfosize szfilename,null lpvoid pblock malloc dwsize getfileversioninfo...