So, you want to be able to save your data and read it back? Well you've come to the right place!
The wiki is a very useful thing, but putting all the separate functions together in a working program
can be a daunting task, so this tutorial should hopefully teach you to use the file IO (fs) API via
a fully working program.
Where to start?
First, we need a program to make. What i'm going to do is a program which asks a user for their
display name, and their real name, then when they input their display name, the real name is used
to talk back to them. This is redundant on its own, but used in a multi-user system, it could come in
handy. But anyway, it should illustrate the point.
Getting input
So now we start writing! First we need to ask the user for their display name, and real name.
term.clear() -- Clears any text from the screen.
term.setCursorPos(1,1) -- Set the cursor to the top left of the screen.
print("What is your display name?") -- Just prints the string to the screen.
local dispName = read() -- Declares a variable for the display name, and saves what they type into it.
print("Okay, "..dispName.." what is your real name?") -- Asks for the real name, putting the display name in the middle via the two ".." marks.
local realName = read() -- Same as getting the display name, but with a new variable.
Okay, so now we have the display name, and real name stored, time to save them to a file!
Opening and writing to a file
A quick word here on how this works, you open the file in memory in either "write" or "append" mode
when you want to add content to it. The difference is that write will wipe the file first, whereas append
will just add content to the end of the file. Since we will be adding names, we want it in append mode.
fs.makeDir("saves") -- Here we make the folder the saves will go in.
local file = fs.open("saves/"..dispName,"a") -- This opens the file with the users name in the folder "saves" for appending.
file.writeLine(realName) -- Put the real name in the file.
file.close() -- Allows the file to be opened again by something else, and stops any corruption.
Simple! So we just open a file with the players user name, and append his real name to it. This allows us
to just open the file and read the first line… but that's the next section!
NOTE: at this point, if you run the program, then do "cd saves" then "edit yourdisplayname" you should see whatever you
put for your real name, ready to be read!
Reading the data
To read data, we need to open the file again, but this time we open it in read mode ("r") and use
a different function on the file we receive.
file = fs.open("saves/"..dispName,"r") -- Open the file we used before, ready for reading.
local fileData = {} -- Declare a table to use to hold data.
local line = file.readLine() -- This function reads the next line in the file, until the end.
repeat -- Start a loop which will read lines into a table until no lines are left.
table.insert(fileData,line) -- Puts the value of the current line into the table we have.
line = file.readLine() -- read the next line
until line == nil -- readLine() returns nil when the end of the file is reached.
file.close() -- Close up the file ready for use again.
local realNameFromFile = fileData[1] -- If all has gone well, the real name should be first as that is all that was in the file!
print("I have saved and read your name, "..realNameFromFile)
That looks like a lot of code, but the comments should explain it pretty well.
We could have just read the first line and used that as the real name, but the loop can be reused in
other programs with more than one line of data in their files so that you can get all of it into a nice table.
Done!
Here's the whole program on pastie because it's easier to read!
So I hope this helped a few people, if you have any problems, feel free to post in this thread, or even give
me a PM on the forums and i'll do what I can to help. If there are any other tutorials you would like, say that
too, quite enjoyed writing this so i'll do some more if people want them!