2012年12月1日 星期六

C/C++計算檔案大小

常常不記得取得檔案長度該怎麼寫,所以先筆記下來。

C++
ifstream is; is.open ("test.txt", ios::binary)
is.seekg (0, ios::end);
length = is.tellg();
is.seekg (0, ios::beg);

C
FILE *fp = fopen(file, "r");
fseek(fp, 0, SEEK_END);
int size = ftell(fp);
fseek(fp, 0, SEEK_SET);

沒有留言:

張貼留言