Writing to the middle of a file
-
I am trying to open a file using fopen for append and switching its current position using fseek to modify its current contents.
oFile=fopen(strcat(f\_SELF,"1.exe"), "a+b"); fseek(oFile,f\_POS,SEEK\_SET); putw(0x00000000,oFile); //fwrite((void\*)VALUE,4,1,oFile); //putw((int)VALUE,oFile); fclose(oFile);
When I use fwrite on the file it returns a C0000005 exception and with the current code the new file info is appended to the bottom of the file instead of overwritten in the middle. How do I write to were my pointer is set?
-
I am trying to open a file using fopen for append and switching its current position using fseek to modify its current contents.
oFile=fopen(strcat(f\_SELF,"1.exe"), "a+b"); fseek(oFile,f\_POS,SEEK\_SET); putw(0x00000000,oFile); //fwrite((void\*)VALUE,4,1,oFile); //putw((int)VALUE,oFile); fclose(oFile);
When I use fwrite on the file it returns a C0000005 exception and with the current code the new file info is appended to the bottom of the file instead of overwritten in the middle. How do I write to were my pointer is set?
From MSDN:
When a file is opened with the "a" or "a+" access type, all write
operations occur at the end of the file. The file pointer can be
repositioned using fseek or rewind but is always moved back to the end of
the file before any write operation is carried out. Thus, existing data
cannot be overwritten.When the "r+", "w+", or "a+" access type is specified, both reading and
writing are allowed (the file is said to be open for “update”). However,
when you switch between reading and writing, there must be an intervening
fflush, fsetpos, fseek, or rewind operation. The current position can be
specified for the fsetpos or fseek operation, if desired.So instead of using append mode try to use r+ it is for both reading and writing. Hope this helps. -- modified at 0:31 Monday 18th December, 2006
-
From MSDN:
When a file is opened with the "a" or "a+" access type, all write
operations occur at the end of the file. The file pointer can be
repositioned using fseek or rewind but is always moved back to the end of
the file before any write operation is carried out. Thus, existing data
cannot be overwritten.When the "r+", "w+", or "a+" access type is specified, both reading and
writing are allowed (the file is said to be open for “update”). However,
when you switch between reading and writing, there must be an intervening
fflush, fsetpos, fseek, or rewind operation. The current position can be
specified for the fsetpos or fseek operation, if desired.So instead of using append mode try to use r+ it is for both reading and writing. Hope this helps. -- modified at 0:31 Monday 18th December, 2006
-
Using w+ overwrites the previous file, so I get an x size file because the data between 0x00 to where where fseek is set is filled with 0's and whatever was in fputw is at the end. I need to reserve the previous contents too.