Programming question under Windows XP

I’m trying to write a computer program to write a binary file, specifically a pgm file, on a Windows XP platform. However, I’m having the following problem: every time I want to write the byte x0A, the program precedes it with the byte x0D in my file. My guess is that the operating system is responsible, since x0A is the ASCII code of the line feed character, which is supposed to always be preceded by a carriage return (x0D) in Windows text files, and because it does this whether I write the code in C++ or Perl, even if I use methods that are not supposed to format the data (ostream’s put method in C++, for example). Is there any way to disable this behaviour of my operating system?

You’re getting the translation of the carriage-returns. This happens when you open a file in text mode. When using the function ‘fopen’, be sure to open in binary mode to prevent this:

fopen(“file.txt”, “wb”);

Like emarkp says, you need to choose binary mode when you open the file.

Yup, like emarkp said. Or if you’re using C++:


 
ofstream outputFile("test.pgm", ios::binary);

I figured there was a flag I could set, but I didn’t think that it had to be done at the opening of the file. I should have thought about it though, so thanks everyone. I’ll make the necessary changes when I get back to my work.