獲取檔案大小的幾種方式

2021-08-09 20:01:46 字數 1826 閱讀 1184

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\n", size );

}

bogon:~ zhaojunyan$ ./main 

the size of file is 1091b

bogon:~ zhaojunyan$ ls -l | grep server.cpp 

-rw-r--r--   1 zhaojunyan  staff   1091 10 25 15:53 server.cpp

bogon:~ zhaojunyan$ 

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

#include #include using namespace std;

int file_size2(char* filename)

int main(int argc, char const *argv)

;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標準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 d...

獲取檔案大小

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 ...