Replacing some bytes in a file in binary mode
-
Hi, I have a file which is in binary mode.(I am using stdio functions fwrie,fread,fseek) I have to read this file in binary mode and needs to replace some of the bytes in this file. It contains around 1000 entries with 2 bytes each. I know the location of bytes which is to be changed, like 100th entry. Is there anyway to change that specific bytes, without copying in to other file. As I have small memory I can not copy into other file and recopy with changed bytes. So on the fly i want to change specific bytes. please tell me if any method is there to do this.?
Regards, Sunil Kumar
-
Hi, I have a file which is in binary mode.(I am using stdio functions fwrie,fread,fseek) I have to read this file in binary mode and needs to replace some of the bytes in this file. It contains around 1000 entries with 2 bytes each. I know the location of bytes which is to be changed, like 100th entry. Is there anyway to change that specific bytes, without copying in to other file. As I have small memory I can not copy into other file and recopy with changed bytes. So on the fly i want to change specific bytes. please tell me if any method is there to do this.?
Regards, Sunil Kumar
You may open the file for read/write (i.e.
"r+"
) go to intended position withfseek
and change the byte content withfwrite
. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
You may open the file for read/write (i.e.
"r+"
) go to intended position withfseek
and change the byte content withfwrite
. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
My code (beware: error checking omitted for concision)
FILE * fp = fopen("foo.bin", "rb+");
fseek(fp, 10, SEEK_SET);
fwrite("#", 1, 1, fp);
fclose(fp);is working fine. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]modified on Friday, January 22, 2010 6:43 AM
-
did you remember fflush() the buffers?
-
My code (beware: error checking omitted for concision)
FILE * fp = fopen("foo.bin", "rb+");
fseek(fp, 10, SEEK_SET);
fwrite("#", 1, 1, fp);
fclose(fp);is working fine. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]modified on Friday, January 22, 2010 6:43 AM
Except that for binary you could do a "rb+".
«_Superman_» I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++) -
As superman's said elsewhere in this thread, use binary mode when you open the file, using
FILE * fp = fopen("foo.txt", "rb+");
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p CodeProject MVP for 2010 - who'd'a thunk it!
-
Except that for binary you could do a "rb+".
«_Superman_» I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++)Oh, thanks... :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]