如何得知檔案大小? (C/C++)

 1 /* 
 2(C) OOMusou 2007 http://oomusou.cnblogs.com
 3
 4Filename    : ArrayCopyCout.cpp
 5Compiler    : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++
 6Description : Demo how to get file size
 7Release     : 03/04/2007 1.0
 8*/

 9 #include  < iostream >
10 #include  < fstream >
11
12 using   namespace  std;
13 int  main()  {
14  ifstream file("clena.bmp",ios::in | ios::binary | ios::ate);
15  ifstream::pos_type eof = file.tellg();
16  cout << "file size : " << eof << "bytes" << endl;
17  
18  file.close();
19}


執行結果

file size : 786486bytes


ios::ate會將檔案指標移到檔尾,透過ifstream::tellg()得知目前檔案指標位置,即相當於檔案大小byte數。

或許C++還有更好的方法,這是我目前學習ifstream時得知的方式,若有更好的方式歡迎指正,謝謝。

你可能感兴趣的:(c/c++)