Lua

Reading and Writing Files

Reading A Single Line From a File:

Suppose that you have a file that has multiple lines of text, and you want to read a line of text from that file, and display it. We can use the io.open() command to open the file. 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. The first thing we will do is to open the file and store opened file (so to speak) in a variable.
file = io.open("testRead.txt", "r")
Now that the file testRead.txt has been opened, it is ready to be read. We will use the read() command to read a single line from the file into another variable.
ourline = file:read()
This reads the first line of our text file. The next time we use the read() command it will read the second line. The next time, it would read the third line - and so on. Notice that we use the name of our file variable along with this command, separated by a colon. There are also some arguments you can use with file:read(). You can put any of the following arguments inside the parentheses with quotation marks. Now that we've read our line, we need to close the file we opened, which we do like this:
file:close()

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.
file = io.open("testRead.txt","r")
for line in file:lines() do
    print(line)
    end
file:close()

This code simply outputs the content of the file a line at a time, to the Output Pane (remember though that the Output Pane is only visible if you run a plugin the Plugin Editor).

Writing To Files (Overwrite):

Writing to files is done in much the same way. The first method shown here will overwrite anything previously written in that file:
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 after all the text that is already in that file, instead of replacing it all. This is done in exactly the same way as the above, except that we use a mode, for append.

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

You may have noticed that this time, in the myText variable, we added \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. Some text file viewers may not display the line break correctly (it may be display on the same line as the surrounding text, as a little square), but technically it is a new line. The same method is used in C/C++.