Diferences while saving int value through fwrite
Dec 15, 2008 at 9:51pm UTC
Hi
Following code
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <stdio.h>
int main(int argc, char ** argv)
{
int i = 9;
FILE* fp;
if ((fp = fopen("data.dat" ,"w" )) == NULL)
{
perror("blad\n" );
}
fwrite(&i,sizeof (int ),1,fp);
fclose(fp);
return 0;
}
saves in file following data (hex): 09 00 00 00
but when I change i initialization from 9 to 10 then in file I have this:
0D 09 00 00 00
Can someone describe why there is "0D" at the beginning?
P.S. I'm using Microsoft Windows XP Sp3, Visual C++ 2005 EE
Thanks
Wehs
Dec 15, 2008 at 9:57pm UTC
If you mean 0D 0A 00 00 00
it is because you need to open the file in binary mode ("wb")
because it is treating as text and converting your unix newline
(0x0a) into a DOS newline (0x0d 0x0a).
Dec 15, 2008 at 9:58pm UTC
Yes, there should be "0D 0A 00 00 00".
Thanks
Wehs
Topic archived. No new replies allowed.