Overwrite a single byte in a file Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
pneumatic
Posts: 338
Joined: 05 Dec 2016, 01:51

Overwrite a single byte in a file

13 Apr 2021, 14:08

As per the title, is this possible in Autohotkey?

I've been trying for hours with VarSetCapacity, NumPut and RawWrite to no avail.

I have a very small test file which contains 46 bytes:

Image

I would like to write, say, 0xFF to the cell at offset 0x0A.

i.e the value 0x80 in cell 0x0A in the above image should change to 0xFF, without changing any of the other bytes around it.

My latest attempt:

Code: Select all

VarSetCapacity(byte , 1)
NumPut(0xFF, byte, 0, "Char")
file := FileOpen( "myfile.txt" , "w")
file.Pos := 0x0A
file.RawWrite(byte, 1)
file.close()
teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: Overwrite a single byte in a file  Topic is solved

13 Apr 2021, 14:56

I'd suggest you learning the basics before deal with raw hex data, this may be dangerous! ;)
FileOpen() —> Flags wrote:w    Write: Creates a new file, overwriting any existing file.
You need at least using "rw". Although your method will work with "rw", more suitable way is:

Code: Select all

file := FileOpen("myfile.txt", "rw")
file.Pos := 0x0A
file.WriteUChar(0xFF)
file.Close()
pneumatic
Posts: 338
Joined: 05 Dec 2016, 01:51

Re: Overwrite a single byte in a file

13 Apr 2021, 16:34

teadrinker wrote:
13 Apr 2021, 14:56
I'd suggest you learning the basics before deal with raw hex data, this may be dangerous! ;)
FileOpen() —> Flags wrote:w    Write: Creates a new file, overwriting any existing file.
You need at least using "rw". Although your method will work with "rw", more suitable way is:

Code: Select all

file := FileOpen("myfile.txt", "rw")
file.Pos := 0x0A
file.WriteUChar(0xFF)
file.Close()
Brilliant, thank you!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot] and 340 guests