C++:使用ifstream读取大型pgm文件

标签 c++ ifstream pgm

我想使用 std::ifstream 加载大小为 4096x4096 像素的 16 位二进制 PGM 图像。问题是我只能加载较小的文件,例如。 512x512。如果我尝试加载“大”数据,我得到的数据对于每个像素始终为 0。

示例代码:

int size = width*height;
unsigned short* data = new unsigned short[size];

// Read the terrain data
for(int i = 0; i < size; i++)
{
    file >> data[i];
}

如果我手动将大小设置为较低的值,这似乎可行。有什么想法吗?

谢谢提姆

最佳答案

operator >> 不应用于二进制提取操作。相反,通过使用 read 文件将简单地输入字节:

file.read(reinterpret_cast<char*>(data), sizeof data);

关于C++:使用ifstream读取大型pgm文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17391528/

相关文章:

c++ - 从 Qt Creator 运行应用程序给出错误 QXcbConnection 然后中止

c++ - 具有重复输入的文件流

c# - 在 .pgm 图像中提取颜色位的最快方法?

c - 如何读取.pgm文件并使用malloc分配内存?

python - 使用opencv编写pgms时如何更改最大灰度的默认值?

c++ - dylib如何卸载自身?

c++ - 在 opencv 中使用网络摄像头未显示图像

c++ - OpenGL 中的 BMP 屏幕截图

c++ - 在 C++ 中以十六进制读取文件

C++ 在内存中加载二进制文件并获取对象