File Operations: Help with fwrite

Jonathan Wakely jwakely.gcc@gmail.com
Mon Jan 3 13:18:00 GMT 2011


On 3 January 2011 09:21, Rohit Arul Raj wrote:
> Hello All,
>
> I need some clarification for fwrite.

This question has nothing to do with GCC so this mailing list is the
wrong place to ask.  You should find a general C programming forum.

> #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?

Yes.

>    If yes, then will using fwrite change the file mode to binary?

No.

Text mode affects how line-endings are written on some platforms, but
you haven't said what platform you're using.  It might be that a byte
with the same value as '\n' is being converted to a different sequence
of bytes.



More information about the Gcc-help mailing list