Latest web development tutorials

Lua file I / O

Lua I / O libraries for reading and processing files. Divided into simple patterns (like C), full mode.

  • Simple mode (simple model) has a current input file and a current output file, and provide for these file-related operations.
  • Full mode (complete model) using an external file handle to achieve. It is in a face in the form of an object, all file operations will be defined as a method of file handles

In simple mode to do some simple file operations when more appropriate. But after some advanced file operation, the simple model appeared to be inadequate. For example while reading this operation multiple files, use the Full mode is more suitable.

The open file operation statement is as follows:

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

Value mode are:

mode description
r Opened read-only file, the file must exist.
w Open the write-only file, if the file exists, the file length is cleared to 0, that is, the contents of the file will disappear. If the file does not exist, create the file.
a In additional write-only file is opened. If the file does not exist, the establishment of the file, if the file exists, the write data will be added to the end of the file, the contents of the original file will be retained. (EOF character reserved)
r + Both reading and writing to open a file, the file must exist.
w + Open to read and write files, if the file exists, the file length of zero clear that the contents of the file will disappear. If the file does not exist, create the file.
a + And a similar, but this file is readable and writable
b Binary mode, if the file is a binary file, you can add b
+ Sign indicates a file can either read and write

Simple Mode

Simple mode using the standard I / O or use a current input file and a current output file.

The following is file.lua file code, file operation test.lua (if you do not need to create the file), as follows:

-- 以只读方式打开文件
file = io.open("test.lua", "r")

-- 设置默认输入文件为 test.lua
io.input(file)

-- 输出文件第一行
print(io.read())

-- 关闭打开的文件
io.close(file)

-- 以附加的方式打开只写文件
file = io.open("test.lua", "a")

-- 设置默认输出文件为 test.lua
io.output(file)

-- 在文件最后一行添加 Lua 注释
io.write("--  test.lua 文件末尾注释")

-- 关闭打开的文件
io.close(file)

Implementation of the above code, you will find that the first line of information test.ua output file and the last line in the file add lua comments. I am here as output is:

-- test.lua 文件

In the example above we used io "x", wherein io.read (), we do not have parameters, parameter can be one of the following table:

mode description
"* N" A number is read and returned it. Example: file.read ( "* n")
"* A" Read the entire file from its current location. Example: file.read ( "* a")
"* L" (default) Reads the next line at end of file (EOF) at returns nil. Example: file.read ( "* l")
number Returns a specified number of characters in the string, or when EOF returns nil. Example: file.read (5)

Other io methods are:

  • io.tmpfile (): returns a handle to the temporary file, the file is opened in update mode automatically deleted when the program ends

  • io.type (file): detecting whether a obj file handles available

  • io.flush (): write buffer all the data to the file

  • io.lines (optional file name): Returns an iterator function, each call will get a line in the file, when the end of the file, it will return nil, but does not close the file


Full Mode

Usually we need to deal with multiple files at the same time. We need to use the file: function_name instead io.function_name methods. The following example demonstrates the same file at the same time as:

-- 以只读方式打开文件
file = io.open("test.lua", "r")

-- 输出文件第一行
print(file:read())

-- 关闭打开的文件
file:close()

-- 以附加的方式打开只写文件
file = io.open("test.lua", "a")

-- 在文件最后一行添加 Lua 注释
file:write("--test")

-- 关闭打开的文件
file:close()

Implementation of the above code, you will find that the first line of information test.ua output file and the last line in the file add lua comments. I am here as output is:

-- test.lua 文件

read parameters consistent with the simple mode.

Other methods:

  • file: seek (optional whence, optional offset): set and retrieve the current file position, success, returns the final file position (in bytes), fails nil plus an error message is returned.Whence parameter values ​​can be:

    • "Set": From the beginning of the file
    • "Cur": From the current position [default]
    • "End": starting from end of file
    • offset: The default is 0
    Without parameters file: seek () returns the current location, file: seek ( "set") is targeted to the file header, file: seek ( "end") to locate the end of the file and returns the file size
  • file: flush (): write buffer all the data to the file

  • io.lines (optional file name): open the specified file filename read mode and returns an iterator function, each call will file a line, when the end of the file, it will return nil, and automatically close the file.
    If the belt parameters io.lines () <=> io.input (): lines (); the default input device to read the contents, but the end does not close the file, such as

    for line in io.lines("main.lua") do
    
      print(line)
    
      end
    

The following example uses seek methods to locate the file penultimate position 25 and use the read method * a parameter, read the entire file from the current location (the penultimate position 25).

-- 以只读方式打开文件
file = io.open("test.lua", "r")

file:seek("end",-25)
print(file:read("*a"))

-- 关闭打开的文件
file:close()

My result is output here:

st.lua 文件末尾--test