标准C++方法获取文件长度

14 views
Skip to first unread message

bruc...@gmail.com

unread,
Oct 13, 2005, 10:04:21 PM10/13/05
to 星星爱CPP
#include <fstream>
#include <cstdio>
using namespace std;

int main( void )
{
fstream file( "D:\\张国荣.JPG", ios_base::binary | ios_base::in
);
if( file.is_open() )
{
// ... // 这里可以进行其它操作
fstream::pos_type cur_pos = file.tellg();
file.seekg( 0L, ios::end );
fstream::pos_type end_pos = file.tellg();
file.seekg( cur_pos, ios::beg );
streamoff flen = end_pos; // 注1
printf( "%I64d\n", flen );
}
return 0;
}
// 注1: 在gcc3.4.2中 streamoff 是 int64_t.
// 而在VC++6.0中 streamoff 是 long, 所以除了标准的
// operator fposstreamoff() const 外, 又提供了一个
// fpos_t get_fpos_t() const, fpos_t 是 __int64.

Reply all
Reply to author
Forward
0 new messages