c++ - 从二进制文件读取并存储到缓冲区

标签 c++ file binary line

有人能告诉我这是否正确吗? 我尝试逐行读取二进制文件并将其存储在缓冲区中?它存储在缓冲区中的新行是否删除了先前存储的行?

        ifs.open(filename, std::ios::binary);
        for (std::string line; getline(ifs, line,' '); )
                {
                    ifs.read(reinterpret_cast<char *> (buffer), 3*h*w);

                }

最佳答案

出于某种原因,您将基于文本的读取的 getline 和二进制读取的 read() 混合在一起。

此外,完全不清楚什么是 buffer 以及它的大小。因此,这里有一个简单的示例供您开始:

ifs.open(filename, std::ios::binary); // assume, that everything is OK

constexpr size_t bufSize = 256;
char buffer[bufSize];
size_t charsRead{ 0 };
do {
    charsRead = ifs.read(buffer, bufSize)
    // check if charsRead == 0, if it's ok
    // do something with filled buffer.
    // Note, that last read will have less than bufSize characters,
    // So, query charsRead each time.
} while (charsRead == bufSize);

关于c++ - 从二进制文件读取并存储到缓冲区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40908659/

相关文章:

c++ - 优化一个简单的 2D Tile 引擎(+潜在错误修复)

java - 目录中的文件如何在 Java 中列出?

C++ 类继承和赋值运算符

parsing - 如何以二进制流格式解析 OpenFoam polyMesh?

C : Reading bytes from binary file

c++ - Arduino - C++ 对象构造函数错误

C++ 在 do-while 循环中不需要的暂停

c++ - 使用 __int16(或 int16_t)优于 int 的优点/缺点

Java:在不需要管理权限的情况下在哪里编写配置

Android逐行读取大文件