Adr*_*thy 23 c++ binaryfiles istream
我试图使用ifstream逐字节读取二进制文件.我之前使用像get()之类的istream方法一次读取二进制文件的整个块而没有问题.但是我目前的任务有助于逐字节地进行,并依靠io系统中的缓冲来提高效率.问题是我似乎比我应该更快地到达文件的末尾几个字节.所以我写了以下测试程序:
#include <iostream>
#include <fstream>
int main() {
typedef unsigned char uint8;
std::ifstream source("test.dat", std::ios_base::binary);
while (source) {
std::ios::pos_type before = source.tellg();
uint8 x;
source >> x;
std::ios::pos_type after = source.tellg();
std::cout << before << ' ' << static_cast<int>(x) << ' '
<< after << std::endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这会转储test.dat的内容,每行一个字节,显示前后的文件位置.
果然,如果我的文件碰巧有两个字节的序列0x0D-0x0A(对应于回车和换行),则跳过这些字节.
Windows上的MSVC++ 2008.
Jam*_*nze 21
>>提取器用于格式化输入; 他们跳过空格(默认情况下).对于单字符无格式输入,您可以使用
istream::get()(int如果读取失败则返回一个EOF,或者[0,UCHAR_MAX]范围内的值)或istream::get(char&)(将参数中的字符读入,返回转换为的内容
bool,如果读取成功,如果失败则返回false.
为什么使用格式化提取而不是.read()?