struct stat结构体的详解和用法
生活随笔
收集整理的這篇文章主要介紹了
struct stat结构体的详解和用法
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
[cpp]?view plaincopy
S_ISREG(st_mode):是否是一個常規(guī)文件.
S_ISDIR(st_mode):是否是一個目錄
S_ISCHR(st_mode):是否是一個字符設(shè)備.
S_ISBLK(st_mode):是否是一個塊設(shè)備
S_ISFIFO(st_mode):是否 是一個FIFO文件.
S_ISSOCK(st_mode):是否是一個SOCKET文件?
[cpp]?view plaincopy
[cpp]?view plaincopy
- //!?需要包含de頭文件??
- ??
- #include?<sys/types.h>??
- ??
- #include?<sys/stat.h>???
?
S_ISLNK(st_mode):是否是一個連接.S_ISREG(st_mode):是否是一個常規(guī)文件.
S_ISDIR(st_mode):是否是一個目錄
S_ISCHR(st_mode):是否是一個字符設(shè)備.
S_ISBLK(st_mode):是否是一個塊設(shè)備
S_ISFIFO(st_mode):是否 是一個FIFO文件.
S_ISSOCK(st_mode):是否是一個SOCKET文件?
?
?
?
- ??
- int?stat(const?char?*filename,?struct?stat?*buf);?//!?prototype,原型???
- ??
- struct?stat??
- {??
- ??
- ????dev_t???????st_dev;?????/*?ID?of?device?containing?file?-文件所在設(shè)備的ID*/??
- ??
- ????ino_t???????st_ino;?????/*?inode?number?-inode節(jié)點號*/??
- ??
- ????mode_t??????st_mode;????/*?protection?-保護模式?*/??
- ??
- ????nlink_t?????st_nlink;???/*?number?of?hard?links?-鏈向此文件的連接數(shù)(硬連接)*/??
- ??
- ????uid_t???????st_uid;?????/*?user?ID?of?owner?-user?id*/??
- ??
- ????gid_t???????st_gid;?????/*?group?ID?of?owner?-?group?id*/??
- ??
- ????dev_t???????st_rdev;????/*?device?ID?(if?special?file)?-設(shè)備號,針對設(shè)備文件*/??
- ??
- ????off_t???????st_size;????/*?total?size,?in?bytes?-文件大小,字節(jié)為單位*/??
- ??
- ????blksize_t???st_blksize;?/*?blocksize?for?filesystem?I/O?-系統(tǒng)塊的大小*/??
- ??
- ????blkcnt_t????st_blocks;??/*?number?of?blocks?allocated?-文件所占塊數(shù)*/??
- ??
- ????time_t??????st_atime;???/*?time?of?last?access?-最近存取時間*/??
- ??
- ????time_t??????st_mtime;???/*?time?of?last?modification?-最近修改時間*/??
- ??
- ????time_t??????st_ctime;???/*?time?of?last?status?change?-?*/??
- ??
- };??
[cpp]?view plaincopy
- #include?<iostream>??
- ??
- #include?<ctime>??
- ??
- #include?<sys/types.h>??
- ??
- #include?<sys/stat.h>???
- ??
- using?namespace?std;???
- ??
- int??
- main?()??
- {??
- ????struct?stat?buf;??
- ??
- ????int?result;??
- ??
- ????result?=?stat?("./Makefile",?&buf);??
- ??
- ????if?(result?!=?0)??
- ??????{??
- ??????????perror?("Failed?^_^");??
- ??????}??
- ????else??
- ??????{??
- ??
- ??????????//!?文件的大小,字節(jié)為單位??
- ??
- ??????????cout?<<?"size?of?the?file?in?bytes:?"?<<?buf.st_size?<<?endl;??
- ??
- ??????????//!?文件創(chuàng)建的時間??
- ??
- ??????????cout?<<?"time?of?creation?of?the?file:?"?<<?ctime?(&buf.st_ctime)?<<??
- ??
- ??????????????endl;??
- ??
- ??????????//!?最近一次修改的時間??
- ??
- ??????????cout?<<?"time?of?last?modification?of?the?file:?"?<<??
- ??
- ??????????????ctime?(&buf.st_mtime)?<<?endl;??
- ??
- ??????????//!?最近一次訪問的時間??
- ??
- ??????????cout?<<?"time?of?last?access?of?the?file:?"?<<?ctime?(&buf.st_atime)??
- ??
- ??????????????<<?endl;??
- ??????}??
- ??
- ????return?0;??
- ??
- }??
[cpp]?view plaincopy
- $?./test??
- ??
- size?of?the?file?in?bytes:?36??
- ??
- time?of?creation?of?the?file:?Sun?May?24?18:38:10?2009??
- ??
- time?of?last?modification?of?the?file:?Sun?May?24?18:38:10?2009??
- ??
- time?of?last?access?of?the?file:?Sun?May?24?18:38:13?2009 ?
?
轉(zhuǎn)載于:https://www.cnblogs.com/wxmdevelop/p/4649495.html
總結(jié)
以上是生活随笔為你收集整理的struct stat结构体的详解和用法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 黄金是多少钱一克啊?
- 下一篇: 背景音乐的实现