标准C++方法获取文件长度 [补充]

6 views
Skip to first unread message

bruc...@gmail.com

unread,
Nov 16, 2006, 10:29:48 PM11/16/06
to 星星爱CPP
[原帖]http://groups.google.com/group/bruceteen/browse_thread/thread/90ef833dfbad9ab6

[补充]
#include <iostream>
#include <fstream>
using namespace std;

fpos_t filelen( ifstream& file )
{
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 );
return end_pos.seekpos();
}

int main( void )
{
ifstream infile( "s.dat" );
if( infile.is_open() )
{
fpos_t len = filelen( infile );
cout << "文件长度" << len << endl;
}
else
{
cout << "文件不存在" << endl;
}
}

---

Reply all
Reply to author
Forward
0 new messages