Page 1 of 1
insert into file without overwriting
Posted: 06 Dec 2012, 03:12
by maackey
So, my question isn't directly related to spring, but more of lua in general.
Is there a way I can easily insert eg. a string into a file without overwriting the contents?
I tried something like this:
file:write(abcdefghijklmn)
local bookmark = file:seek()
file:write(stuvwxyz)
file:seek("set", bookmark)
file:write(opqr)
but instead of getting output
I get
I've written spaces to "fill the gap" in the meantime, but since "opqr" is generated dynamically later on in the file it is very hard to predict how long it will be.
Re: insert into file without overwriting
Posted: 06 Dec 2012, 04:11
by Forboding Angel
As food for thought, depending on what you are doing, it might be better to have a config file that gets included wherever the values are needed as a go between. That way you're not risking overwriting important stuff. And having one lua file write directly into another is O_o
That said, couldn't you just use a global variable?
Re: insert into file without overwriting
Posted: 06 Dec 2012, 04:45
by knorke
Is there a way I can easily insert eg. a string into a file without overwriting the contents?
I dont think such thing is possible because of how files work?
imo read file into array, insert your stuff, write it again.
if you feel like being cautious you could write to a temp file and when everything worked, replace the orginal file with that.
or if you just want to add at the end of the file, "a" parameter is for append.
Re: insert into file without overwriting
Posted: 06 Dec 2012, 05:37
by zwzsg
knorke wrote:imo read file into array
Or read the whole file into a string. I have a widget that read files in the 50kb range into a string, apply some gsub to it, then write it to a file.
Re: insert into file without overwriting
Posted: 12 Dec 2012, 05:11
by maackey
Thanks guys! I managed to get it working perfectly
Forboding Angel wrote:As food for thought, depending on what you are doing, it might be better to have a config file that gets included wherever the values are needed as a go between. That way you're not risking overwriting important stuff. And having one lua file write directly into another is O_o
That said, couldn't you just use a global variable?
Its a lua file that generates verilog code. I can't use a variable, because I don't know how many wires there are and what they will be named.
knorke wrote:or if you just want to add at the end of the file, "a" parameter is for append.
Can't append to the end of the file as the wires need to be defined before I use them ^^
zwzsg wrote:r read the whole file into a string. I have a widget that read files in the 50kb range into a string, apply some gsub to it, then write it to a file.
Yeah apparently lua is a beast at strings.
Because Lua handles long strings efficiently, a simple technique for writing filters in Lua is to read the whole file into a string, do the processing to the string (typically with gsub), and then write the string to the output:
t = io.read("*all") -- read the whole file
t = string.gsub(t, ...) -- do the job
io.write(t) -- write the file
ended up using this, worked perfectly.
I attached the file if anyone is interested in seeing what it does/how horribly noobish I am at lua.