獲取檔案大小的幾種方式

2021-09-25 15:48:18 字數 3091 閱讀 9131

**:

1、通過檔案c標準io庫函式fseek、fell獲得,間接獲得,速度慢

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

fseek(fp, 0, seek_end);

size = ftell(fp);

fclose(fp);

printf("the size of file is %db\n", size );

}

2、通過檔案狀態資訊獲取,直接獲取,速度快

#include #include using namespace std;

int file_size2(char* filename)

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

;

可以有兩種方法來獲取乙個檔案的屬性:

1、通過路徑:

int stat(const char *path, struct stat *struct_stat);

int lstat(const char *path,struct stat *struct_stat);

兩個函式的第乙個引數都是檔案的路徑,第二個引數是struct stat的指標。返回值為0,表示成功執行。

執行失敗是,error被自動設定為下面的值:

ebadf: 檔案描述詞無效

eloop: 遍歷路徑時遇到太多的符號連線

enametoolong:檔案路徑名太長

enoent:路徑名的部分元件不存在,或路徑名是空字串

enomem:記憶體不足

enotdir:路徑名的部分元件不是目錄

這兩個方法區別在於stat沒有處理字元鏈結(軟鏈結)的能力,如果乙個檔案是符號鏈結,stat會直接返回它所指向的檔案的屬性;而lstat返回的就是這個符號鏈結的內容。這裡需要說明一下的是軟鏈結和硬鏈結的含義。我們知道目錄在linux中也是乙個檔案,檔案的內容就是這這個目錄下面所有檔案與inode的對應關係。那麼所謂的硬鏈結就是在某乙個目錄下面將乙個檔名與乙個inode關聯起來,其實就是新增一條記錄!而軟鏈結也叫符號鏈結更加簡單了,這個檔案的內容就是乙個字串,這個字串就是它所鏈結的檔案的絕對或者相對位址。

3、通過檔案描述符

int fstat(int fdp, struct stat *struct_stat);  //通過檔案描述符獲取檔案對應的屬性。fdp為檔案描述符

下面是這個結構的結構

struct stat {

mode_t     st_mode;       //檔案對應的模式,檔案,目錄等

ino_t      st_ino;       //inode節點號

dev_t      st_dev;        //裝置號碼

dev_t      st_rdev;       //特殊裝置號碼

nlink_t    st_nlink;      //檔案的連線數

uid_t      st_uid;        //檔案所有者

gid_t      st_gid;        //檔案所有者對應的組

off_t      st_size;       //普通檔案,對應的檔案位元組數

time_t     st_atime;      //檔案最後被訪問的時間

time_t     st_mtime;      //檔案內容最後被修改的時間

time_t     st_ctime;      //檔案狀態改變時間

blksize_t st_blksize;    //檔案內容對應的塊大小

blkcnt_t   st_blocks;     //偉建內容對應的塊數量

stat結構體中的st_mode 則定義了下列數種情況:

s_ifmt   0170000    檔案型別的位遮罩

s_ifsock 0140000    scoket

s_iflnk 0120000     符號連線

s_ifreg 0100000     一般檔案

s_ifblk 0060000     區塊裝置

s_ifdir 0040000     目錄

s_ifchr 0020000     字元裝置

s_ififo 0010000     先進先出

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

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

s_isvtx 01000     檔案的sticky位

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

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

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

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

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

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

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

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

s_ixoth 00001             其他使用者具可執行許可權

上述的檔案型別在posix中定義了檢查這些型別的巨集定義:

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

s_isreg (st_mode)    是否為一般檔案

s_isdir (st_mode)    是否為目錄

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

s_isblk (s3e)        是否為先進先出

s_issock (st_mode)   是否為socket

若一目錄具有sticky位(s_isvtx),則表示在此目錄下的檔案只能被該檔案所有者、此目錄所有者或root來刪除或改名,在linux中,最典型的就是這個/tmp目錄啦

獲取檔案大小的幾種方式

1 通過檔案c標準課fseek fell獲得,間接獲得,速度慢 include include int main int argc,char argv fseek fp,0,seek end size ftell fp fclose fp printf the size of file is db ...

獲取檔案大小

1 找到檔案所在的絕對路徑。系統路徑 projectpath request.getsession getservletcontext getrealpath 檔案路徑 在上傳檔案的時候,一定會有檔案相對路徑,如果不清楚,可以打斷點然後在本地找到此檔案。檔案相對路徑 filepath 絕對路徑rea...

VC獲取檔案大小

vc獲取檔案大小 我查到兩種方法 方法一 win32 find data fileinfo handle hfind dword filesize const char filename 檔案的路徑及名字 hfind findfirstfile filename fileinfo if hfind ...