Jump to content




Basic file IO - Learn how to save data!

help lua

46 replies to this topic

#1 ben657

  • Members
  • 65 posts

Posted 06 September 2012 - 08:24 PM

Basic file IO - Learn how to save data!

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!

#2 ETHANATOR360

  • Members
  • 423 posts
  • Locationyour hardrive

Posted 06 September 2012 - 09:40 PM

nice tutorial fixed up some code for you :D/>
before:
[color="#000000"]term[/color][color="#666600"].[/color][color="#000000"]clear[/color][color="#666600"]()[/color] [color="#666600"]--[/color][color="#000000"] [[[/color][color="#660066"]Clears[/color][color="#000000"] any text [/color][color="#000088"]from[/color][color="#000000"] the screen[/color][color="#666600"].]][/color]
[color="#000088"]print[/color][color="#666600"]([/color][color="#008800"]"What is your display name?"[/color][color="#666600"])[/color] [color="#666600"]--[[[/color] [color="#660066"]Just[/color][color="#000000"] prints the [/color][color="#000088"]string[/color][color="#000000"] to the screen[/color][color="#666600"].[/color] [color="#666600"]]][/color]
[color="#000000"]dispName [/color][color="#666600"]=[/color][color="#000000"] read[/color][color="#666600"]()[/color] [color="#666600"]--[[[/color] [color="#660066"]Declares[/color][color="#000000"] a variable [/color][color="#000088"]for[/color][color="#000000"] the display name[/color][color="#666600"],[/color] [color="#000088"]and[/color][color="#000000"] saves what they type [/color][color="#000088"]into[/color][color="#000000"] it[/color][color="#666600"].[/color] [color="#666600"]]][/color]
[color="#000088"]print[/color][color="#666600"]([/color][color="#008800"]"Okay, "[/color][color="#666600"]..[/color][color="#000000"]dispName[/color][color="#666600"]..[/color][color="#008800"]" what is your real name?"[/color][color="#666600"])[/color] [color="#666600"]--[[[/color] [color="#660066"]Asks[/color] [color="#000088"]for[/color][color="#000000"] the real name[/color][color="#666600"],[/color][color="#000000"] putting the display name [/color][color="#000088"]in[/color][color="#000000"] the middle via the two [/color][color="#008800"]".."[/color][color="#000000"] marks[/color][color="#666600"].]][/color]
[color="#000000"]realName [/color][color="#666600"]=[/color][color="#000000"] read[/color][color="#666600"]()[/color] [color="#666600"]--[[[/color] [color="#660066"]Same[/color] [color="#000088"]as[/color][color="#000000"] getting the display name[/color][color="#666600"],[/color][color="#000000"] but [/color][color="#000088"]with[/color][color="#000000"] a [/color][color="#000088"]new[/color][color="#000000"] variable[/color][color="#666600"].[/color] [color="#666600"]]][/color]
[color="#666600"]
[/color]
after:
[/color]
[color="#666600"][color="#000000"]term[/color][color="#666600"].[/color][color="#000000"]clear[/color][color="#666600"]()[/color] [color="#666600"]-- [/color][color="#660066"]Clears[/color][color="#000000"] any text [/color][color="#000088"]from[/color][color="#000000"] the screen[/color][color="#666600"].[/color]
[color="#000088"]term.setCursorPos (1,1)[/color][/color]
[color="#666600"][color="#000088"]print[/color][color="#666600"]([/color][color="#008800"]"What is your display name?"[/color][color="#666600"])[/color] [color="#666600"]--[/color] [color="#660066"]Just[/color][color="#000000"] prints the [/color][color="#000088"]string[/color][color="#000000"] to the screen[/color][color="#666600"].[/color]
[color="#000000"]local dispName [/color][color="#666600"]=[/color][color="#000000"] read[/color][color="#666600"]()[/color] [color="#666600"]-- [/color][color="#660066"]Declares[/color][color="#000000"] a variable [/color][color="#000088"]for[/color][color="#000000"] the display name[/color][color="#666600"],[/color] [color="#000088"]and[/color][color="#000000"] saves what they type [/color][color="#000088"]into[/color][color="#000000"] it[/color][color="#666600"].[/color]
[color="#000088"]print[/color][color="#666600"]([/color][color="#008800"]"Okay, "[/color][color="#666600"]..[/color][color="#000000"]dispName[/color][color="#666600"]..[/color][color="#008800"]" what is your real name?"[/color][color="#666600"])[/color] [color="#666600"]--[/color] [color="#660066"]Asks[/color] [color="#000088"]for[/color][color="#000000"] the real name[/color][color="#666600"],[/color][color="#000000"] putting the display name [/color][color="#000088"]in[/color][color="#000000"] the middle via the two [/color][color="#008800"]".."[/color][color="#000000"] marks[/color][color="#666600"].[/color]
[color="#000000"]local realName [/color][color="#666600"]=[/color][color="#000000"] read[/color][color="#666600"]()[/color] [color="#666600"]-- [/color][color="#660066"]Same[/color] [color="#000088"]as[/color][color="#000000"] getting the display name[/color][color="#666600"],[/color][color="#000000"] but [/color][color="#000088"]with[/color][color="#000000"] a [/color][color="#000088"]new[/color][color="#000000"] variable[/color][color="#666600"].[/color] [/color]
[color="#666600"]

double brackets are use for multi line stuff such as
-- [[this is a multi
line comment]]
always put local before variables because if its not local it can be accsesed from other programs and that can cause aalot of incompatibilities
EDIT:not sure why all the html stuff is showing up in the quotes

#3 ben657

  • Members
  • 65 posts

Posted 06 September 2012 - 10:11 PM

Thanks for those corrections, I'll put them in once I'm back on my computer, far too fiddly with this phone!

As for the comments, I generally just do them like that on this forum for the look of it... Although I suppose it is a bit redundant... And normal ones might be easier to read, will change that too... And get put the next section in!

#4 ETHANATOR360

  • Members
  • 423 posts
  • Locationyour hardrive

Posted 06 September 2012 - 10:25 PM

will the next section be reading the files? because if it is im very exicted

#5 Noodle

  • Members
  • 989 posts
  • LocationSometime.

Posted 07 September 2012 - 12:55 AM

Thankyou for not actually using io.
Turns out that io is a wrapper around fs (as quoted from wiki).

#6 ben657

  • Members
  • 65 posts

Posted 07 September 2012 - 07:32 AM

Added the final section, and fixed up a couple of things, mostly the corrections given by ethan, and making the directory before saving to avoid errors.

#7 Luanub

    Lua Nub

  • Members
  • 1,135 posts
  • LocationPortland OR

Posted 07 September 2012 - 07:50 AM

View PostNoodle, on 07 September 2012 - 12:55 AM, said:

Thankyou for not actually using io.
Turns out that io is a wrapper around fs (as quoted from wiki).

Where did you read that? I don't see it anywhere on the wiki and it is backwards.

IO is the default Lua input / output API, and FS is an API for CC.

That's why you can find info for IO in the Official 5.1 Lua Reference manual, but FS is not mentioned.

Edit: Okay I found where it states that on the wiki. Still find it rather odd that a default API is actually a wrapper around a CC API. I guess if they severely modified the IO API that could be the case.

Edited by luanub, 07 September 2012 - 07:58 AM.


#8 ben657

  • Members
  • 65 posts

Posted 07 September 2012 - 07:58 AM

View Postluanub, on 07 September 2012 - 07:50 AM, said:

View PostNoodle, on 07 September 2012 - 12:55 AM, said:

Thankyou for not actually using io.
Turns out that io is a wrapper around fs (as quoted from wiki).

Where did you read that? I don't see it anywhere on the wiki and it is backwards.

IO is the default Lua input / output API, and FS is an API for CC.

That's why you can find info for IO in the Official 5.1 Lua Reference manual, but FS is not mentioned.

I thought it was this way round too... although I do remember the wiki used to say something about io being a wrapper...

#9 Tiin57

    Java Lunatic

  • Members
  • 1,412 posts
  • LocationIndiana, United States

Posted 07 September 2012 - 04:22 PM

View PostNoodle, on 07 September 2012 - 12:55 AM, said:

Thankyou for not actually using io.
Turns out that io is a wrapper around fs (as quoted from wiki).
You still sore about Cloudy's rebuttal, then?

#10 ben657

  • Members
  • 65 posts

Posted 07 September 2012 - 04:30 PM

View Posttiin57, on 07 September 2012 - 04:22 PM, said:

View PostNoodle, on 07 September 2012 - 12:55 AM, said:

Thankyou for not actually using io.
Turns out that io is a wrapper around fs (as quoted from wiki).
You still sore about Cloudy's rebuttal, then?
Have I like, started up some old argument with this thread or..? xD

#11 Noodle

  • Members
  • 989 posts
  • LocationSometime.

Posted 07 September 2012 - 09:22 PM

View Posttiin57, on 07 September 2012 - 04:22 PM, said:

View PostNoodle, on 07 September 2012 - 12:55 AM, said:

Thankyou for not actually using io.
Turns out that io is a wrapper around fs (as quoted from wiki).
You still sore about Cloudy's rebuttal, then?
What rebuttal?
I don't remember one 0.0
People on my server told me IO was a wrapper around fs.. I said io is actual Lua ?

#12 Cloudy

    Ex-Developer

  • Members
  • 2,543 posts

Posted 07 September 2012 - 10:16 PM

IO is a wrapper round FS - however I don't understand why using IO would be a bad thing?

IO is there to provide compatibility between the sandboxed FS and the Lua IO library. Learning the IO library would be much more useful since it isn't CC specific.

#13 ben657

  • Members
  • 65 posts

Posted 07 September 2012 - 10:32 PM

No offence to you guys discussing IO and fs, but could you not do it on this thread? It's more for help regarding the tutorial, or feedback.

#14 Noodle

  • Members
  • 989 posts
  • LocationSometime.

Posted 07 September 2012 - 11:57 PM

View PostCloudy, on 07 September 2012 - 10:16 PM, said:

IO is a wrapper round FS - however I don't understand why using IO would be a bad thing?

IO is there to provide compatibility between the sandboxed FS and the Lua IO library. Learning the IO library would be much more useful since it isn't CC specific.
Its not a bad thing, its just "incorrect" to teach about when the actual filesystem is FS not IO in CC.

#15 Cloudy

    Ex-Developer

  • Members
  • 2,543 posts

Posted 08 September 2012 - 10:36 AM

View PostNoodle, on 07 September 2012 - 11:57 PM, said:

View PostCloudy, on 07 September 2012 - 10:16 PM, said:

IO is a wrapper round FS - however I don't understand why using IO would be a bad thing?

IO is there to provide compatibility between the sandboxed FS and the Lua IO library. Learning the IO library would be much more useful since it isn't CC specific.
Its not a bad thing, its just "incorrect" to teach about when the actual filesystem is FS not IO in CC.

But it isn't. IO is universal between things that use Lua - so teaching IO is more correct.

To OP: nice work - but I'm sure you see my point about IO :D/>

#16 ben657

  • Members
  • 65 posts

Posted 08 September 2012 - 12:04 PM

View PostCloudy, on 08 September 2012 - 10:36 AM, said:

But it isn't. IO is universal between things that use Lua - so teaching IO is more correct.

To OP: nice work - but I'm sure you see my point about IO :D/>
Thanks :D/> and yeah, but you both have points. IO is obviously more powerful since it works outside of computercraft, on your actual system, but for the sake of saving files using CC in-game, then fs is perfectly fine, and a little simpler (although I admit I haven't looked much into the IO api).

#17 MrBarry

  • Members
  • 29 posts
  • LocationColorado, USA

Posted 14 September 2012 - 06:58 PM

Deleted: stupid question. Sorry.

#18 Mr. Fang

  • Members
  • 82 posts

Posted 18 September 2012 - 08:14 PM

This is actually something awesome! Thanx for the code...Imma make a door lock for this now!

#19 ben657

  • Members
  • 65 posts

Posted 18 September 2012 - 09:28 PM

View PostMr. Fang, on 18 September 2012 - 08:14 PM, said:

This is actually something awesome! Thanx for the code...Imma make a door lock for this now!

Because its not computer craft unless it's got a door lock :D/>

But thanks for the comments, good to know its okay, first tutorial I've written :)/>

#20 Sariaz

  • Members
  • 107 posts

Posted 23 September 2012 - 07:22 AM

and so based of this I'm assuming that if i wanted more data id just do more write lines with the data or would it be like the write function and add to current line and if it dosnt is there anyway to append on to a previously written line besides reading it from file apending to it then rewriting old line?





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users