English read/write

16 replies
Goto Page
To the start Previous 1 Next To the start
27.02.11 02:50:05 pm
Up
EnderCrypt
User
Offline Off
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
WARNING: Found unknown Windows version: Windows 7
27.02.11 03:13:51 pm
Up
KaBooMa
User
Offline Off
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.
27.02.11 03:18:06 pm
Up
EnderCrypt
User
Offline Off
ah... now i andurstand, ok also how to read/write on lets say..

next line?

thx
WARNING: Found unknown Windows version: Windows 7
27.02.11 03:19:01 pm
Up
Yasday
User
Offline Off
\n is the newline character.
edited 1×, last 27.02.11 03:37:41 pm
27.02.11 03:23:37 pm
Up
EnderCrypt
User
Offline Off
ok where to place it if i for example wanna skip 5 lines
WARNING: Found unknown Windows version: Windows 7
27.02.11 03:37:33 pm
Up
Yasday
User
Offline Off
io.write(player(id,"health").."\n\n\n\n\n")
Like this.
27.02.11 03:58:28 pm
Up
EnderCrypt
User
Offline Off
reading then?
WARNING: Found unknown Windows version: Windows 7
27.02.11 04:00:06 pm
Up
SQ
Moderator
Offline Off
Why didn't you searched for some simple tutorial instead?
http://www.evilmana.com/tutorials/lua_tutorial_09.php

Reading all file lines from file stream would be...
Code:
1
2
3
4
for line in f:lines() do
     file.lines[i] = line
     i = i + 1
end
edited 3×, last 27.02.11 04:09:56 pm
27.02.11 04:08:23 pm
Up
EnderCrypt
User
Offline Off
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
WARNING: Found unknown Windows version: Windows 7
28.02.11 01:23:47 pm
Up
Yasday
User
Offline Off
Instead of "w" use "a".
28.02.11 03:35:28 pm
Up
EnderCrypt
User
Offline Off
doesent that overwrite old text as i use file:write(\n) ?
WARNING: Found unknown Windows version: Windows 7
28.02.11 03:47:46 pm
Up
KimKat
GAME BANNED
Offline Off
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.
Code:
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
28.02.11 03:59:42 pm
Up
EnderCrypt
User
Offline Off
no i want to write on a certain line, i got the line number in a variable, now i wanna write to it
WARNING: Found unknown Windows version: Windows 7
28.02.11 04:21:58 pm
Up
Flacko
User
Offline Off
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)
28.02.11 04:27:26 pm
Up
EnderCrypt
User
Offline Off
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?
WARNING: Found unknown Windows version: Windows 7
28.02.11 04:33:49 pm
Up
Lee
Moderator
Offline Off
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.
28.02.11 04:39:07 pm
Up
EnderCrypt
User
Offline Off
ok thx
WARNING: Found unknown Windows version: Windows 7
To the start Previous 1 Next To the start