计算文件字节数的方法
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
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
相关文章推荐
- 将分区表students子分区sex_male中年龄大于30的记录的classid修改为40
- 允许debian wheezy支持IOS7+的iphone.
- 第4周—项目2 建立“单链表”算法库
- 多个线程操作StringBuffer和StringBuilder
- Java注解阐述
- butterknife+dagger+otto+okhttp+picasso+retrofit+rxjava示例项目
- 将字符串反转
- python应用之二分递归实现sqrt,递归实现快排
- 抓包 Wireshark
- 多线程
- 安装maven
- matlab switch case 和 try catch用法示例
- windows命令
- Mybatis系列(八)集合映射
- 《TCP/IP详解 卷一:协议》读书笔记--链路层
- K-means算法
- effective c++ 以对象管理资源
- UVA 11426 GCD - Extreme (II)
- Java设计模式之封装算法的模板模式
- jquery 获取元素内容和值