std::ifstream::readsome的原型如下,可以返回实际读到的字节数量,但是不会把eofbit置1,所以不能直接调用eof判断是否到文件尾
streamsize readsome (char* s, streamsize n);
std::ifstream::read原型如下,会置eof, 但没法直接得到实际读取字节数
istream& read (char* s, streamsize n);
如果既需要能获得读取字节数,又需要能判断是否读到文件尾,可有如下两种方法:
1. 使用std::ifstream::readsome
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
streamsize Read(istream &stream, char *buffer, streamsize count)
{
// This consistently fails on gcc (linux) 4.8.1 with failbit set on read
// failure. This apparently never fails on VS2010 and VS2013 (Windows 7)
streamsize reads = stream.rdbuf()->sgetn(buffer, count);
// This rarely sets failbit on VS2010 and VS2013 (Windows 7) on read
// failure of the previous sgetn()
stream.rdstate();
// On gcc (linux) 4.8.1 and VS2010/VS2013 (Windows 7) this consistently
// sets eofbit when stream is EOF for the conseguences of sgetn(). It
// should also throw if exceptions are set, or return on the contrary,
// and previous rdstate() restored a failbit on Windows. On Windows most
// of the times it sets eofbit even on real read failure
stream.peek();
return reads;
}
int main(int argc, char *argv[])
{
ifstream instream("filepath", ios_base::in | ios_base::binary);
while (!instream.eof())
{
char buffer[0x4000];
size_t read = Read(instream, buffer, sizeof(buffer));
// Do something with buffer
}
}
2. 使用std::ifstream::read后,使用gcount()获得上一次读取的字节数
ifstream ifs("a.txt");
char buf[1024];
ifs.read(buf, 1024);
size_t extracted = ifs.gcount();
// or
ifstream ifs("a.txt");
char buf[1024];
size_t extracted = ifs.read(buf, 1024).gcount();
Ref:
https://stackoverflow.com/questions/9191876/when-does-ifstreamreadsome-set-eofbit
https://stackoverflow.com/questions/11720880/ifstreamread-doesnt-tell-how-many-bytes-it-really-reads