File Operations: Help with fwrite

Rohit Arul Raj rohitarulraj@gmail.com
Mon Jan 3 09:21:00 GMT 2011


Hello All,

I need some clarification for fwrite.

#include<stdio.h>

int main()
{
 unsigned long writeBuffer [64];
 unsigned long Pos;
 unsigned long j;

 FILE* pFile = fopen("tmp.txt", "w+t");

 for(j = 0; j < 64; j++)
   writeBuffer[j] = 2560 + j * 4;             // -----------------------(A)

 fwrite(writeBuffer, 1, 256, pFile);

 fgetpos(pFile, &Pos);
 printf("pos = %d\n", Pos);

 fclose (pfile);
}

-------------------------------------------
Expected output : 256
Actual Output    :  320
---------------------------------------------

1) Can i use fwrite even though the file has been opened in text mode?
   If yes, then will using fwrite change the file mode to binary?

2) i do get the expected output for all the below mentioned cases:
   a) open the file in binary mode.
   b) Change 2560 to 0 in statement [A].
   c) Write an unsigned char instead of an unsigned int.

I understand that the way data's get stored using fprintf and fwrite
are different. But still if i use fwrite and dump 256 bytes of data in
to a file, fgetpos should have returned 256?

Any one can help me with this?

Thanks,
Rohit



More information about the Gcc-help mailing list