Sony Playstation Portable Lua Programming: Reading and Writing of Files
In this lesson we are going to learn how to read from files, as well as write to files. This can be very handy in making games. You can use this to pull chunks of code from other files, as well as save information to files such as high scores, game saves, player stats etc...
Let's get started!

Uses of Reading and Writing Files
First, let's look at a very simple command called dofile.
This command will read code from another file and immediately execute it.

dofile("./test.lua")

The command is as simple as that. Note that the ./ does not mean the file is in a subdirectory. If the file was, we would use this:

dofile("./files/test.lua")

So, if our file test.lua contained this code:

playerx = 10
playery = 20
enemyx = 40
enemyy = 50

...then as soon as we called that file with the dofile command, those variables would get declared. You do not have to use lua files. txt files or any other are fine.

Reading A Single Line From a File:
Let's say you have a file that has multiple lines of text, and you want to read one line of text from that file, and print it to the screen.
For this we can use the io.open() command to open the file, then read from it.
This command has the syntax of io.open(filename, mode).
The mode we will use for this will be r, for read mode.
For reference here is a list of all modes available.

  • r - read mode
  • w - write mode (overwrites existing)
  • a - append mode (appends to existing)
  • b - binary mode
  • r+ - update mode (existing data preserved)
  • w+ - update mode (existing data erased)
  • a+ - append update mode (existing data preserved, append at end of file only)
The first thing we will do is open the file and store it in a variable. Look below.

file = io.open("testRead.txt", "r")

Now our file testRead.txt has been opened, and is ready for read mode.
Next, we will use the read() command to read a single line from the file into another variable.

ourline = file:read()

This will read the first line of our text file. The next time we use the read() command it will read the second line. Next, it would read the third line, and so on.
Notice that we use the name of our variable that our file is opened with along with this command.
There are also some arguments you can use with file.read().
You can put any of the below arguments inside the parentheses with quotation marks.

  • *n - reads a number and returns it. ex: file.read("*n")
  • *a - reads the entire file from the current position. ex: file.read("*a")
  • *l - (default) - reads the next line, returns nil on End of File (EOF). ex: file.read("*l")
  • number - returns a string with up to that many characters in it, or nil on EOF. ex: file.read(5)
Now that we've read our line, we need to close the file we opened, like so:

file:close()

Now, to print our file to the screen we would simply use this:

screen:print(10,10,ourline,white)

Reading All Lines:
To read all the lines in a file you could use the same technique with a for statement. Look at the code below.

y = 10

file = io.open("testRead.txt","r")

for line in file:lines() do
y = y + 10
screen:print(100,y,line,white)
end

file:close()

This code sets up a starting y value to use for the printing, so that it will print each line at a different y on the screen.
We open our file the exact same way.
Then we come to the for statement. This line is saying for how ever many lines are in the file, do the following code. Our y gets added by 10 for each loop and is used in the printing command to print it below the next line.

Writing To Files (Overwrite):
Writing to files is done about the same way. Our first method here will overwrite anything previously written in that file. Here's the code:

file = io.open("testRead.txt","w")
file:write("hello")
file:close()

This time notice we used the w mode instead of r since we're writing, and not reading.
We use file:write() with the text we want written to the file in parentheses and quotes. You could also use a variable instead. If you use a variable do not use quotation marks. Here's an example.

file = io.open("testRead.txt","w")
myText = "Hello"
file:write(myText)
file:close()
Writing To Files (Append):
You can use append mode so that when you write text to a file it gets added to the bottom of the text that is already in that file, instead of deleting it all. This is done exactly the same as the above except that we use a mode, for append. Look below.

file = io.open("testRead.txt","a")
myText = "\nHello"
file:write(myText)
file:close()

Notice something else different? In the myText variable we added in \n inside the text string.
This command is a line break, and will cause the text to go to the next line when written to our file. It may show up in your file on the same line with a little square in between it, but technically it is a new line. This exact command is also used in C/C++.

Take what you've learned here and try to use it in a small program.
Our next tutorial we cover using sound, and will use the reading and writing commands we have learned here to make a small program.

Functions | Sound

Please welcome mayclemaf, our newest member.

Who's Online: 3 Guests, 0 Users

Total Members: 519
Total Posts: 12867
Total Topics: 1462
Total Categories: 8
Total Boards: 35

Recent Posts:

Re: Extended ASCII by Nielkie
Extended ASCII by Aphonia
Re: Lua 5.1.4 help. by yaustar
Re: Lua 5.1.4 help. by Buddy4point0
Re: Lua 5.1.4 help. by yaustar
Re: Lua 5.1.4 help. by PL3X
Re: Lua 5.1.4 help. by Buddy4point0
Re: Lua 5.1.4 help. by tacticalpenguin


 
Copyright © 2006-2007 www.EvilMana.com All rights reserved.
EvilMana Logo by emcp and Charlie