Jump to content

Overwrite a file without destroying the previous contents


Go to solution Solved by Musashi,

Recommended Posts

Posted

I want modify some bytes in one big file, there are a way to do that?

There are:

$FO_APPEND (1) = Write mode (append to end of file)

$FO_OVERWRITE (2) = Write mode (erase previous contents)

But not a Read/Write mode to go to a position, modify some bytes and leave the rest intact.

The $FO_BINARY (16) = Force binary mode, don't help.

  • Solution
Posted (edited)

Or maybe this :

#include <Constants.au3>

Global $bInsert = "0xFFFFFF"
Global $hFile = FileOpen("mslogo.jpg", BitOR($FO_APPEND, $FO_BINARY))
FileSetpos($hFile, 20, $FILE_BEGIN ) ; Pos = 20
FileWrite($hfile, $bInsert)
FileClose($hFile)

==> Info : A better way to work with binary data is to use a struct !

I would advise you to always create a backup of the file before you change it - safety first ;)

Edited by Musashi

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Posted

And you can also use other methods, especially if this is just a text file.

A very simple approach if you know your insertion point, and it is based on unique text, is to FileRead your file, then use StringSplit with the unique text, then add all three parts together and then FileWrite back to the file with that, overwriting it. The only tricky bit, is if you are not familiar with an array, which is what StringSplit creates.

But as suggested, back your file up first, just in case.

Have a play, and any further questions, keep asking here.

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Posted

Thanks all. Since my file isn't a text file, I need to open it as $FO_APPEND+$FO_BINARY.

If it opens as a text file by default, writing adds an unwanted extra chr(15) (?).

I use this to change the channel mask (4 bytes) of a wav file (maybe larger than 10 GB). Rewriting the entire file takes time and space.

Posted

Code used:

$fh = FileOpen($wav_file, 17)   ; $FO_APPEND (1) + $FO_BINARY (16) = 17
FileSetPos($fh, $pos_mask, 0)   ; $FILE_BEGIN (0) = Beginning of the file.
FileWrite($fh, number($mask,1)) ; $NUMBER_32BIT (1) = 32bit integer (to be sure always 4 bytes)
FileClose($fh)

Posted

Goodo.

I remember having issues once upon a time (some years ago) with surround sound (multiple channel) wav files not being recognized as such, due to some header issue etc.

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...