Wrong color in .ppm file I'm going through the rasterizer tutorial from Scratchapixel, but I encountered a problem with the generation of the .ppm file. Since something was off, I reduced the example to the bare minimum, so I currently simply generate a 512x512 image of a plain color.

#include &lt;fstream&gt;

using Rgb = unsigned char[3];
int main()
{
    const uint32_t w = 512;
    const uint32_t h = 512;

    Rgb *framebuffer = new Rgb[w * h];
    for (uint32_t j = 0; j &lt; h; ++j) {
        for (uint32_t i = 0; i &lt; w; ++i) {
            framebuffer[j * w + i][0] = static_cast&lt;unsigned char&gt;(255);
            framebuffer[j * w + i][1] = static_cast&lt;unsigned char&gt;(0);
            framebuffer[j * w + i][2] = static_cast&lt;unsigned char&gt;(0);
        }
    }

    std::ofstream ofs;
    ofs.open("./example.ppm");
    ofs &lt;&lt; "P6\n" &lt;&lt; w &lt;&lt; " " &lt;&lt; h &lt;&lt; "\n255\n";
    ofs.write((char*)framebuffer, w * h * 3);
    ofs.close();

    delete[] framebuffer;

    return 0;
}


The code above should generate a red square image, but if I open the file with Gimp, I get a green image.



Do you have any idea of what's happening?

EDIT: This is the .ppm file with a hex editor:
The issue is that on your system, writing out "\n" is writing out 0x0D0A, whereas on my system, it's only writing out 0x0A. If you explicitly write out 0x0A instead of "\n" I think it will work for you.

Here's a hex dump on macOS Mojave:

50 36 0A 35 
31 32 20 35 
31 32 0A 32 
35 35 0A 
FF 00 00 // &lt;- pixels start here
FF 00 00 
FF 00 00 
FF 00 00 
FF 00 00 
FF 00 00