Wrong color in .ppm file
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