Question

I want to load a 16-bit binary PGM image with a size of 4096x4096 pixels using std::ifstream. The problem is that I can only load smaller files, eg. 512x512. If I try to load the "large" one the data I get is always 0 for every pixel.

Example Code:

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];
}

If I set size manually to a lower value, this seems to work. Any idea?

Thx Tim

Was it helpful?

Solution

operator >> should not be used for binary extraction operations. Instead, by using read the file will simply input the bytes:

file.read(reinterpret_cast<char*>(data), sizeof data);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top