我想使用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];
}
如果我将大小手动设置为一个较低的值,这似乎可行。任何想法?
蒂姆·蒂姆(Thx Tim)
最佳答案
operator >>
不应用于二进制提取操作。相反,通过使用read
文件将仅输入字节:
file.read(reinterpret_cast<char*>(data), sizeof data);
关于c++ - C++:使用ifstream读取大型pgm文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17391528/