为什么std :: istream :: read根据读取的字节移动文件指针NOT

时间:2014-10-13 14:58:45

标签: c++ fstream

我是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?

知道为什么会这样吗?

1 个答案:

答案 0 :(得分:1)

问题是由于这一行:

std::fstream ifInput(pathSrc.string().c_str());

我正在阅读的文件部分是二进制的,所以:

std::fstream ifInput(pathSrc.string().c_str(), std::fstream::in | std::fstream::binary);

解决了这些问题。

相关问题