linux c下獲取檔案大小

2021-08-02 21:49:56 字數 1077 閱讀 6921

一般在讀取檔案中的內容的時候,如果你不知道到底分配多大的空間,為何不先獲取檔案的大小,然後再……

這裡貼出兩種方法:1.

unsigned 

long

get_file_size(

const

char

*path)  

2.

#include 

unsigned 

long

get_file_size(

const

char

*path)  

else

return

filesize;  

}

第一種:通過開啟檔案的方式seek得到檔案大小,這種方式如果檔案比較大,則效率可想而知。而且如果另外乙個程序開啟檔案寫的時候,衝突來了。

第二種:通過c語言的函式stat將檔案先放到buffer中,然後獲取buffer的大小,這種方式效率更高

例子:

struct   stat   buf;

stat   (「/tmp/tmpfile」,&buf);

printf(「/tmp

/tmp

file   size   =   %d   /n」,buf.st_size);

下面給出stat封裝的結構

#ifdef __ext_lf64src

struct stat64 ;

#endif

獲取檔案大小

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

ORACLE獲取檔案大小

set serveroutput on declare ex boolean flen number bsize number begin utl file.fgetattr oraload test.txt ex,flen,bsize if ex then dbms output.put line...