I try to update certain parts of a file without everything else being deleted or overwritten, however the read/write modes of Lua only seem to support either appending data, or deleting existing data and writing the new one. Here’s an example:
testfile.pak: “It’s cold outside” –> “It’s warm outside”
Now, I’m dealing with rather big files, not text-files, so reading them into the memory and just using the string manipulation functions are no options here. Also, I’m already processing the files as part of the program, so I don’t want to add to the overhead by re-reading them up to the necessary point, insert the data and write the rest. This would require twice as much HDD-space and also twice as much time. I also can’t write the necessary data in the first processing cycle. So I’m wondering if it’s possible to just tell Lua to write to a certain offset within a file without having to create a temp-file or a duplicate, kind of like a hex-editor is able to change specific portions of files without having to save a whole new copy of the file. Are there other – may be undocumented – write options for io.open other than “w”, “w+”, “a” and “a+”?
You can do it with the “r+” mode: