您的位置:首页 > 其它

计算文件字节数的方法

2015-10-02 21:52 381 查看
1 调用ifstream打开一个文件

2 调用seekg将get pointer置为文件末尾,seekg(0, ios_base::end)

3 调用tellg获取总字节数,实际上获取的是get pointer相对于文件头的偏移字节数

4 重置get pointer,使其指向文件头,以便执行其他操作seekg (0, ios::beg);

以下代码摘自www.cplusplus.com

[cpp] view
plaincopy

#include <iostream>

#include <fstream>

using namespace std;

int main () {

int length;

char * buffer;

ifstream is;

is.open ("test.txt", ios::binary );

// get length of file:

is.seekg (0, ios::end);

length = is.tellg();

is.seekg (0, ios::beg);

return 0;

}

对于ifstream对象的每一次read过后,可以调用ifstream::gcount获取读取的字节数,

gcount的返回值为streamsize,而streamsize是个整型,signed int或signed long
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: