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

标签 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?

知道这是为什么吗?

最佳答案

问题是由于这一行:

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/

相关文章:

C++ 类重载 : is it possible to make compiler see which one to use based on template?

c++ - 从 v8::Script::Compile() 获取错误消息

c++ - 计算一个 cuda 内核有多少 block 和线程,以及如何使用它们

c++ - 尝试从文本文件读取到对象数组

java - C++ getline() 比 Java 的 readLine() 慢

c++ - 为什么这个数据流在第 26 个字节结束?

c++ - mingw 5.2 带有 std::string 的奇怪消息

c++ - 使用 unique_ptr 成员编写移动构造函数的正确方法(崩溃)

c++ - 我应该使用什么条件来停止文件读取循环?