Forum

> > CS2D > Scripts > Text file modification
Forums overviewCS2D overview Scripts overviewLog in to reply

English Text file modification

14 replies
To the start Previous 1 Next To the start

old Text file modification

AtomKuh
User Off Offline

Quote
I need a lua script that changes the content of a text file each 10 seconds. For instance, once 10 seconds are ran out, the previous empty text file should now contain the letter a. After the next 10 seconds the letter should be changed to b. Afterwards should the letter be changed back to a.

I tried to do it myself but I failed. I don't understand this I/O Model in lua.

I need this script to determine whether my server is still running or not. As a result I can restart automatically my server with another script.

old Re: Text file modification

VADemon
User Off Offline

Quote
1
2
3
4
5
6
7
function updateUptimeFile(path)
	local path = path or "sys/lua/uptime.txt"
	local file = io.open(path, "w")

	file:write(os.date("%H:%M:%S"))
	file:close()
end

old Re: Text file modification

TrialAndError
User Off Offline

Quote
Not really tested, but this is something. This should cycle every 10 seconds and update the text document. There are other ways, but this is just a quick one to get you into thinking.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
local i = 0;
local temp = "a";
addhook("second", "test")
function test()
	i = i + 1;
	if a > 100 then a = 1; end
	if i%10 == 0 then
		local file = io.open("path/to/file", "w");
		if (temp == "a") then temp = "b";
		else temp = "a";
		end
		file:write(temp);
		file:close();
	end
end

old Re: Text file modification

MikuAuahDark
User Off Offline

Quote
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
-- Use locals and upvalues, it's faster
local file = io.open("sys/lua/file.txt", "rb+")
local write_a = false

function updateTextFile()
	-- Start writing on beginning of file
	file:seek("set")
	-- Write either "a" or "b"
	file:write(write_a and "a" or "b")
	-- Apply the changes
	file:flush()
	-- Flip the switch to write either "a" or "b" in next write
	write_a = not(write_a)
end

-- Finally call timer, set count to 0 so it's called infinite times.
timer(10000, "updateTextFile", "", 0)

old Re: Text file modification

VADemon
User Off Offline

Quote
local file = io.open("sys/lua/file.txt", "rb+")

should be:
local file = io.open("sys/lua/file.txt", "wb+")

old Re: Text file modification

AtomKuh
User Off Offline

Quote
user MikuAuahDark has written
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
-- Use locals and upvalues, it's faster
local file = io.open("sys/lua/file.txt", "wb+")
local write_a = false

function updateTextFile()
	-- Start writing on beginning of file
	file:seek("set")
	-- Write either "a" or "b"
	file:write(write_a and "a" or "b")
	-- Apply the changes
	file:flush()
	-- Flip the switch to write either "a" or "b" in next write
	write_a = not(write_a)
end

-- Finally call timer, set count to 0 so it's called infinite times.
timer(10000, "updateTextFile", "", 0)


Can someone edit this code that it closes the text document after writing into it, please? I may have some problems if it stays open with another bash script.

Thanks in advance
edited 2×, last 27.04.17 09:42:52 pm

old Re: Text file modification

AtomKuh
User Off Offline

Quote
My code looks now like this:

Spoiler >


I get this error by using it:
1
LUA ERROR: sys/lua/server.lua:7: attempt to use a closed file

old Re: Text file modification

Yates
Reviewer Off Offline

Quote
@user AtomKuh: That's because the file is opened once, then closed the first time
updateTextFile
is used. It is never opened afterwards.

Frankly the local
file
used as a variable for the whole block scope is shitty practice - even if it's faster. Faster does not always mean better.

old Re: Text file modification

AtomKuh
User Off Offline

Quote
@user Yates: Thanks, but do you have any idea to change it?

The problem I have is that I want to check with the following bash script whether a text file is being edited or not:
Spoiler >


Somehow it works on text files that I edit manually but not on the one that is being edited by this lua script

old Re: Text file modification

Yates
Reviewer Off Offline

Quote
Probably because it's still holding the file in its memory and won't load outside changes.

I wouldn't use a text file to check whether a server is offline. You can ping it to see if it's still responding or not.

old Re: Text file modification

MikuAuahDark
User Off Offline

Quote
user Yates has written
@user AtomKuh: That's because the file is opened once, then closed the first time
updateTextFile
is used. It is never opened afterwards.

Frankly the local
file
used as a variable for the whole block scope is shitty practice - even if it's faster. Faster does not always mean better.


Say no more.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
local write_a = false

function updateTextFile()
	-- Start writing on beginning of file
	local file = io.open("sys/lua/file.txt", "wb+")
	-- Write either "a" or "b"
	file:write(write_a and "a" or "b")
	-- Close the file. Will flush it automatically
	file:close()
	-- Flip the switch to write either "a" or "b" in next write
	write_a = not(write_a)
end

timer(10000, "updateTextFile", "", 0)
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview