Jump to content

Binary file modification

- - - - -

  • Please log in to reply
7 replies to this topic

#1
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts
I read a file one int at a time. Sometimes I need to be able to change the value of a few bytes of the file.
Can it be done without rewriting it from the start? The file can be over 10 mb long, so rewriting all of it just to change one byte doesn't seem efficient.
In essence, what I need to do is something like this:



fread(...);//read sizeof(int) bytes


//go back sizeof(int) bytes


//replace the next sizeof(int) bytes with a new value



I need a sort of "insert mode" fwrite.
Thanks!

#2
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
Do you need insert or overwrite, because these two things are different.

When you open the file with fopen(), use mode "w+"

Quote

Create an empty file for both reading and writing. If a file with the same name already exists its content is erased and the file is treated as a new empty file.

If you want to overwrite, after you read the record, you can back up and write a new one using fseek(), like this:

// Assuming 4 byte integers. ptr is a pointer to an integer. fp is a file pointer.

fread(ptr, 4, 1, fp);

// Back up one record

fseek(fp, -4, SEEK_CUR);

// Edit the value of ptr and overwrite the record

fwrite(ptr, 4, 1, fp);


Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#3
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts
Oh, so fwrite overwrites the bytes? Obviously if the current position is the end of file, fwrite appends the new bytes at the end of the file, but if the current position is somewhere in the middle of the file, fwrite replaces the next bytes with new ones (aka doesn't increase the size of the file). Did I understand correctly?

#4
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
If you rewrite/replace same amount of data, then file's size won't change.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#5
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts
It works, awesome. I tought fwrite added data between CUR_POS and CUR_POS+1, I didn't know it replaced the bytes following CUR_POS.

edit: since I'm here, I'll avoid creating another topic. If, for example, I want to delete the next 100 bytes of the file, how can I do it? By delete I don't mean replace with zeros, I mean eliminate.

#6
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
I imagine you'll have to overwrite the bytes following CUR_POS with the data at CUR_POS + 100 till the end of the file, and end the file after the last rewrite. To my knowledge, there's no way to simply mark the intermediate 100 bytes as gone, unless you get into messing directly in the file system's table of contents, which I myself would not do.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#7
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts
You've been very helpful, thanks ;)

#8
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
My pleasure.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users