我是 std::istream::read根据文件指针移动文件指针(如此处所述 does fstream read/write move file pointer )
我有一段看起来很无辜的代码:
void SomeClass::read(__in uint32_t& Res)
{
std::fstream ifInput(pathSrc.string().c_str());
if (ifInput.fail())
{
LOG_ERROR(L"couldn't read file " << pathSrc.string().c_str());
return;
}
m_Stream = &ifInput;
cout << m_Stream->tellg() << endl;
Res = 0;
char cBuffer[4];
m_Stream->read(&cBuffer[0], 4);
if (m_Stream->fail())
return;
cout << m_Stream->tellg() << endl;
}
奇怪的是,我得到了这个输出:
0
3588 <<<<<< why not 4?
知道这是为什么吗?
最佳答案
问题是由于这一行:
std::fstream ifInput(pathSrc.string().c_str());
我正在读取的文件部分是二进制文件,因此:
std::fstream ifInput(pathSrc.string().c_str(), std::fstream::in | std::fstream::binary);
解决了问题。
关于c++ - 为什么 std::istream::read 不根据读取的字节移动文件指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26342982/