Hello friends

I have a very peculiar problem in a VS2008 MFC-program. I wrote a function dumping the contents of a 1280*1024*USHORT buffer to a file, but to my surprise the file size was never 2621440 bytes, but around 80-100 bytes more.

I changed my fwrite:s in all possible fashions, writing all bytes at once, writing one ushort at the time, writing on byte at the time... but problem remained. So then I thought, maybe fwrite recognizes value 10 as the newline characther ('\n') and replaces it by '\r\n'. And, yes, I was correct!

I did this simple code to trace what happens to all possible ushort values when written to disk using fwrite:

Code:
for (USHORT dum=0; dum<=65535; dum++)
{
FILE *pTestFile;
pTestFile = fopen("Logtest.bin", "w+");
for (int i=0; i<1310720; i++)
   fwrite(&dum, sizeof(USHORT), 1, pTestFile);
fclose(pTestFile);

pTestFile = fopen("Logtest.bin", "r");
fseek(pTestFile, 0, SEEK_END);
int filesize = ftell(pTestFile);
fclose(pTestFile);

if (filesize != 2621440)
   TRACE("%d %d\n",dum, filesize);
}
The output from the TRACE line was:
10 3932160
meaning that three bytes had been written for every USHORT. When opening the particular problem file in my hex editor I was not surprised to find the byte sequence '0D 0A 00' , that is '\r\n\0', repeated all through the file, where I would have wanted only '0A 00'.

This is kind of annoying, when writing a 2.000.000 byte memory block containing random data to a file using one single fwrite operation, I don't want the file to end up containing 2.000.100 bytes just because there happened to be 100 bytes with value 10.

So, to make the long question short: why does fwrite behave like this, and what can I do in order to avoid that fwrite replaces all 10-values in my buffer with \r\n when writing to the file?

Thanks in advance!