Last Updated:

Working with files in Lua (Basic)

Lua (Basic)

Lua has many built-in functions to handle this using an I/O library. The I/O library works with both text and binary files. Lua-agnostic Unicode. This means that all strings are efficiently treated as binary data rather than as any kind of encoding. Because of this, you rarely need to use the Unicode library, except in more specific use cases.

Open and close

Before we start reading or writing, we need to discuss opening and closing files. Your workflow typically involves opening a file, doing something with it, and then explicitly closing it. When you exit the program, the file should also close, but this is considered bad practice.

To open a file:

io.open( [filename] [, [mode]] )

The file name is where the file is located, and the mode is listed in the table below (or after the same mode as in C for fopen). If there is no mode, r.

r(Default) The file opens as read-only.

 

wThe file opens for recording. Overwrites the contents or creates a new file.

 

 

aAdd a file. Write to the end or create a new file. Reposition operations are ignored.

 

 

r+The file opens in read/write mode. The file must exist.

 

 

w++ The file will open for recording. Clears existing content or creates a new file.
a+The file is added to Reading view. Write to the end or create a new file. Move operations affect Reading Mode.

 

 

You can also use modifier b at the end of mode to open the file in binary mode. For example:

io.open( [file], "r+b" )

io.open returns a file handle object. When working with files that open, you must assign a file handle to a variable to do anything useful.

localfilehandle = io.open( «mycontents.txt», «rb» )

Once you're done with the file, you need to close it.

localfilehandle = io.open( "mycontents.txt", "rb")
...
filehandle:close()

Reading files

Once you open your file, you'll be able to read it. Reading is carried out through:

filehandle:read( «[option]» [or: [number]] )

The parameters correspond to the following table for Lua 5.1 and 5.2:

*line*lReads a single line from a file.

 

*all*aReads all content from a file.
*number*nReads the number from the file. This is read to the end of the acceptable sequence.

 

 

[number](integer)Reads [the number of] characters (bytes) from the file. Does not use quotation marks.

 

 

Lua 5.3 adds the following (but is backward compatible):

aReads all content from a file.

 

lReads a single line from a file.
LReads a single line from a file, but retains the end-of-line character(s)

 

 

nReads the number from the file. This is read to the end of the acceptable sequence.

 

 

[number]Reads [number] characters (bytes) from a file. Does not use quotation marks.

 

 

Basically, they all return zero if there is no valid sequence, except for the "all" parameters, which return an empty string as long as the file is valid. Let's see it all in action with test.txt:

123
line 2
123456789

(Note the new line at the end of test.txt)

Let's try each of our reading options and see what we get. We are going to start the whole file over and over again to avoid any problems.

#!/usr/bin/lua5.1

local file = io.open( "test.txt", "p" )

localcontens= file:read( «*all» )
print(contens )

file:close()

The output is:

./file1.lua
123
line 2
123456789

Let's break down all of our options together now that we've seen the basic structure:

#!/usr/bin/lua5.1

print( «*all» )
local file= io.open( «test.txt», «p» )
localcontens= file:read( «*all» )
print(contens )
file:close()

print()
print( "*line" )
local file = io.open( «test.txt», «p» )
local contents = file:read( «*line» )
print( contents )
file:close()

print ()
print( «*number» )
local file= io.open( «test.txt», «p» )
local contents = file:read( «*number» )
print(contents )
file:close()

print()
print( "6" )
local file= io.open( «test.txt», «p» )
local contents = file:read( 6 )
print(contents )
file:close()

The output is:

./file1.lua
*all
123
line 2
123456789

*line
123

*number
123

6
123
li

Let's do the same with Lua 5.3:

#!/usr/bin/lua5.3

print( "a" )
local file= io.open( «test.txt», «p» )
local contents = file:read( «a» )
print( contents )
file:close()

print ()
print( "l" )
local file= io.open( "test.txt", "p" )
local contents= file:read( "l" )
print(contents )
file:close()

print ()
print( "L" )
local file= io.open( «test.txt», «p» local
contents= file:read( «L» )
print (contents )
file:close()

print()
print( "n" )
local file= io.open( «test.txt», «p» )
local contents = file:read( «n» )
print (contents )
file:close()

print()
print( «6» )
local file= io.open( «test.txt», «p» )
local contents= file:read( 6 )
print (contents )
file:close()

And we get:

./file1.lua
a
123
line 2
123456789

l 123

L 123




n 123

6
123
li