This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
File Operations: Help with fwrite
- From: Rohit Arul Raj <rohitarulraj at gmail dot com>
- To: gcc-help <gcc-help at gcc dot gnu dot org>
- Date: Mon, 3 Jan 2011 14:51:39 +0530
- Subject: File Operations: Help with fwrite
- References: <AANLkTikmbNGT5xRFyFkZAxJcAeE14KB5QR3MhgDD5cPe@mail.gmail.com>
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