It is currently August 5th, 2020, 1:08 pm

Is there a way to overwrite a text file?

Discuss the use of Lua in Script measures.
jav26122
Posts: 26
Joined: February 9th, 2015, 8:45 pm

Re: Is there a way to overwrite a text file?

Post by jav26122 »

Is there maybe a way I can find the last line of the file more efficiently to use less cpu?
User avatar
jsmorley
Developer
Posts: 21211
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Is there a way to overwrite a text file?

Post by jsmorley »

Another possibility is that whatever is adding to the log file isn't just "appending" one line at a time to it, but flushing some "cache" of lines out and if you remove them it just puts them back. It's hard to be sure.

Trust me though, all other processes aside, if you open a file as "w" or "w+" in Lua, that will be a zero byte empty file until and unless you write to it with Lua.
User avatar
jsmorley
Developer
Posts: 21211
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Is there a way to overwrite a text file?

Post by jsmorley »

jav26122 wrote:Is there maybe a way I can find the last line of the file more efficiently to use less cpu?
http://rainmeter.net/forum/viewtopic.php?p=108002#p108002
jav26122
Posts: 26
Joined: February 9th, 2015, 8:45 pm

Re: Is there a way to overwrite a text file?

Post by jav26122 »

jsmorley wrote:Another possibility is that whatever is adding to the log file isn't just "appending" one line at a time to it, but flushing some "cache" of lines out and if you remove them it just puts them back. It's hard to be sure.

Trust me though, all other processes aside, if you open a file as "w" or "w+" in Lua, that will be a zero byte empty file until and unless you write to it with Lua.

That's what I was thinking but I can't find anything like that anywhere. It seems like the only option would be to be able to just find the last line of the file using as little cpu as possible. I'm not very good with string manipulation so I'm not sure how to go about this.
jav26122
Posts: 26
Joined: February 9th, 2015, 8:45 pm

Re: Is there a way to overwrite a text file?

Post by jav26122 »


Unfortunately this uses the same method as me... Adding every line to a table in a loop gets very intensive when there's soooo many lines.
User avatar
jsmorley
Developer
Posts: 21211
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Is there a way to overwrite a text file?

Post by jsmorley »

jav26122 wrote: That's what I was thinking but I can't find anything like that anywhere. It seems like the only option would be to be able to just find the last line of the file using as little cpu as possible. I'm not very good with string manipulation so I'm not sure how to go about this.
Its not easy in Lua. While you can open a file more or less as a binary file and "seek" to a particular byte location, it will be tricky then to switch from that to a "line" mode and know how many bytes constitutes the "last line".

I have never tried it, but I guess you could "seek" to the end of the file, then back up one byte at a time til you hit a "linefeed", keeping track of bytes. The capture forward, the number of bytes you just backed up. Take some reading of the various Lua manuals and tutorials, but might be doable.
jav26122
Posts: 26
Joined: February 9th, 2015, 8:45 pm

Re: Is there a way to overwrite a text file?

Post by jav26122 »

I've found the issues....sort of....idk...

I stopped logging to the file and stopped the rainmeter skin and opened the text file and I was very confused... It was 900kb but it was mostly blank except the last few lines that didn't get cut off when I stopped everything. I found that for some reason instead of getting deleted everything is just turned into spaces... Once I deleted all the spaces and saved the file it's size decreased significantly.

Any idea why everything it being converted into spaces instead of just getting deleted?

This is what it looks like when I changed it to file.write('asdf') just to see if that had something to do with it.
https://drive.google.com/file/d/0B7gfCkumvFHhRURJSVA2bU5EUEk/view?usp=sharing

The "?" are spaces but google did some weird thing that changed it
Last edited by jav26122 on February 9th, 2015, 10:24 pm, edited 1 time in total.
User avatar
jsmorley
Developer
Posts: 21211
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Is there a way to overwrite a text file?

Post by jsmorley »

jav26122 wrote:I've found the issues....sort of....idk...

I stopped logging to the file and stopped the rainmeter skin and opened the text file and I was very confused... It was 900kb but it was mostly blank except the last few lines that didn't get cut off when I stopped everything. I found that for some reason instead of getting deleted everything is just turned into spaces... Once I deleted all the spaces and saved the file it's size decreased significantly.

Any idea why everything it being converted into spaces instead of just getting deleted?
No, not really. It has to be due to the program creating and appending the log. Lua just won't do that.
User avatar
jsmorley
Developer
Posts: 21211
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Is there a way to overwrite a text file?

Post by jsmorley »

The key is that the strange behavior stops when you stop GPU-Z from logging, then delete the file, it becomes a zero-byte file. If you keep logging going, you really can't "truncate" it, which tells me GPU-Z is also trying to be efficient, by holding the size of the file as some kind of "buffer" size and seeking to the end of that buffer to write new stuff. Then it "seeks" to that point in the log file and writes. If you have truncated the file, it is just "padding" it so it can get to what it "thinks" is the new starting point.
User avatar
jsmorley
Developer
Posts: 21211
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Is there a way to overwrite a text file?

Post by jsmorley »

Create a batch file or vbs script file to start GPU-Z from the Startup folder, instead of just running the shortcut for GPU-Z...

Have that batch or script file delete the log file, then start GPU-Z.

That way any time you reboot, the log is cleared out.