C語言stat 函式 獲取檔案狀態

2021-06-26 23:00:34 字數 2000 閱讀 1681

相關函式:fstat, lstat, chmod, chown, readlink, utime

標頭檔案:#include #include

定義函式:int stat(const char * file_name, struct stat *buf);

函式說明:stat()用來將引數file_name 所指的檔案狀態, 複製到引數buf 所指的結構中。

下面是struct stat 內各引數的說明:

struct stat

;先前所描述的st_mode 則定義了下列數種情況:

1、s_ifmt 0170000 檔案型別的位遮罩

2、s_ifsock 0140000 scoket

3、s_iflnk 0120000 符號連線

4、s_ifreg 0100000 一般檔案

5、s_ifblk 0060000 區塊裝置

6、s_ifdir 0040000 目錄

7、s_ifchr 0020000 字元裝置

8、s_ififo 0010000 先進先出

9、s_isuid 04000 檔案的 (set user-id on execution)位

10、s_isgid 02000 檔案的 (set group-id on execution)位

11、s_isvtx 01000 檔案的sticky 位

12、s_irusr (s_iread) 00400 檔案所有者具可讀取許可權

13、s_iwusr (s_iwrite)00200 檔案所有者具可寫入許可權

14、s_ixusr (s_iexec) 00100 檔案所有者具可執行許可權

15、s_irgrp 00040 使用者組具可讀取許可權

16、s_iwgrp 00020 使用者組具可寫入許可權

17、s_ixgrp 00010 使用者組具可執行許可權

18、s_iroth 00004 其他使用者具可讀取許可權

19、s_iwoth 00002 其他使用者具可寫入許可權

20、s_ixoth 00001 其他使用者具可執行許可權上述的檔案型別在 posix 中定義了檢查這些型別的巨集定義

21、s_islnk (st_mode) 判斷是否為符號連線

22、s_isreg (st_mode) 是否為一般檔案

23、s_isdir (st_mode) 是否為目錄

24、s_ischr (st_mode) 是否為字元裝置檔案

25、s_isblk (s3e) 是否為先進先出

26、s_issock (st_mode) 是否為socket 若一目錄具有sticky 位 (s_isvtx), 則表示在此目錄下的檔案只能被該檔案所有者、此目錄所有者或root 來刪除或改名.

返回值:執行成功則返回0,失敗返回-1,錯誤**存於errno。

錯誤**:

1、enoent 引數file_name 指定的檔案不存在

2、enotdir 路徑中的目錄存在但卻非真正的目錄

3、eloop 欲開啟的檔案有過多符號連線問題, 上限為16 符號連線

4、efault 引數buf 為無效指標, 指向無法存在的記憶體空間

5、eaccess 訪問檔案時被拒絕

6、enomem 核心記憶體不足

7、enametoolong 引數file_name 的路徑名稱太長

範例#include

#include

main()

執行:/etc/passwd file size = 705

fstat()函式

標頭檔案:#include #include

定義函式:int fstat(int fildes, struct stat *buf);

函式說明:fstat()用來將引數fildes 所指的檔案狀態, 複製到引數buf 所指的結構中(struct stat). fstat()與stat()作用完全相同, 不同處在於傳入的引數為已開啟的檔案描述詞. 詳細內容請參考stat().

返回值:執行成功則返回0, 失敗返回-1, 錯誤**存於errno.

c 獲取檔案資訊 stat函式的使用

stat函式用來獲取指定路徑的檔案或者資料夾的資訊。int stat const char path,struct stat buffer 引數 path 檔案或者資料夾的路徑 buffer 獲取的資訊儲存在記憶體中 返回值 正確 返回0 錯誤 返回 1,具體錯誤碼儲存在errno中 stat結構體...

c 獲取檔案資訊 stat函式的使用

stat函式用來獲取指定路徑的檔案或者資料夾的資訊。cpp view plain copy int stat const char path,struct stat buffer 引數 path 檔案或者資料夾的路徑 buffer 獲取的資訊儲存在記憶體中 返回值 正確 返回0 錯誤 返回 1,具體...

HttpClient獲取響應狀態Status

httpclient獲取響應狀態status 我們httpclient向伺服器請求時,正常情況 執行成功 返回200狀態碼,不一定每次都會請求成功,比如這個請求位址不存在 返回404 伺服器內部報錯 返回500 有些伺服器有防採集,假如你頻繁的採集資料,則返回403 拒絕你請求。這個獲取狀態碼,我們...