Forum

> > CS2D > Scripts > read/write
Forums overviewCS2D overview Scripts overviewLog in to reply

English read/write

16 replies
To the start Previous 1 Next To the start

old read/write

EnderCrypt
User Off Offline

Quote
hi
i would want to write/read string to a file

could someone give me a simple example, that just saves a string
and another example that read, and also to just line

thx

old Re: read/write

KaBooMa
User Off Offline

Quote
Here is a easy script that will save and load your hp!

Spoiler >


Hope you find it somewhat useful. Remember, The save is in your lua folder.

old Re: read/write

EnderCrypt
User Off Offline

Quote
ah... now i andurstand, ok also how to read/write on lets say..

next line?

thx

old Re: read/write

Yasday
User Off Offline

Quote
\n is the newline character.
edited 1×, last 27.02.11 03:37:41 pm

old Re: read/write

Yasday
User Off Offline

Quote
io.write(player(id,"health").."\n\n\n\n\n")
Like this.

old Re: read/write

EnderCrypt
User Off Offline

Quote
yeah i searched like 10 times today but i didnt se this, thx
edit:

also if i wanna overwrite a line with a variable, what do i do cause when i try file:write("\n") then it just overwrite everything, is there anything that allows me to skip line without overwriting it?
edited 2×, last 28.02.11 12:36:52 pm

old Re: read/write

KimKat
GAME BANNED Off Offline

Quote
Not if you use "a+" it will just open existing file and add more to it.
"w+" deletes everything in a file before attempting to write to it. These are more useful, I hope.
1
2
3
4
5
6
path = saves -- The directory where you will place the save.
file_save = {0 = [path/text document.txt]}

function save()
	file:write(file_save[0],"w+")
end

Here's a tutorial for y'all.
SaveTableToFile

old Re: read/write

EnderCrypt
User Off Offline

Quote
no i want to write on a certain line, i got the line number in a variable, now i wanna write to it

old Re: read/write

Flacko
User Off Offline

Quote
Yasday has written
io.write(player(id,"health").."\n\n\n\n\n")

You don't need to concatenate when writing to a file.
Instead you should pass it as different parameters to save yourself from doing needless concatenations.

Quote
also if i wanna overwrite a line with a variable

Ohh, it's very simple, you just have to rewrite the whole file instead of the part you want to edit
It's not possible to write to a part of a file (that's why we got databases, but not in lua)

old Re: read/write

EnderCrypt
User Off Offline

Quote
hm ok, then can i ask, how long does it take for lua to read like 1000 lines and place in a array with the help of a loop?

and how long to write 1000 lines from array using w mode in a loop?

old Re: read/write

Lee
Moderator Off Offline

Quote
less than 1 millisecond

At this point, seeing as how you are using a very inefficient serialization method to begin with, I really doubt that the rumtime complexity of IO is something that you have to worry about.
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview