(Lua notes): Lua file I/O

table of Contents

Lua file I/O

Simple mode

Full mode


Lua file I/O

  • Lua I/O library for reading and processing files
Simple model Have a current input file and a current output file, and provide operations related to these files
Complete model Use external file handle to achieve. It defines all file operations as file handle methods in an object-oriented form
  • Simple mode is more suitable when doing simple file operations. But when doing some advanced file operations, the simple mode appears to be inadequate.
    • For example, to read multiple files at the same time, the full mode is more appropriate.
  • The open file operation statement is as follows:
file = io.open (filename [, mode])
  • The values ​​of mode are:
r To open the file as read-only, the file must exist.
w Open the write-only file, if the file exists, the file length is cleared to 0, that is, the content of the file will disappear. If the file does not exist, create the file.
a Open the write-only file in append mode. If the file does not exist, it will be created. If the file exists, the written data will be added to the end of the file, that is, the original content of the file will be retained. (EOF symbol reserved)
r+ To open the file in read-write mode, the file must exist.
w+ Open a readable and writable file. If the file exists, the file length is cleared to zero, that is, the content of the file will disappear. If the file does not exist, create the file.
a+ Similar to a, but this file is readable and writable
b Binary mode, if the file is a binary file, you can add b
+ The sign indicates that the file can be read or written

Simple mode

  • Simple mode uses standard I/O or uses a current input file and a current output file.
  • The following is the file code of file.lua, the file to be operated on is test.lua (you need to create the file if it does not exist), the code is 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)

输出:-- test.lua 文件
  • In the above example, the io."x" method is used. There is no parameter in io.read() . The parameter can be one of the following:
mode description
"*n" Read a number and return it. Example: file.read("*n")
"*a" Read the entire file from the current position. Example: file.read("*a")
"*l" (default) Read the next line and return nil at the end of the file (EOF). Example: file.read("*l")
number Returns a string with the specified number of characters, or nil at EOF. Example: file.read(5)
  • Other io methods are:
io.tmpfile() Returns a temporary file handle, the file is opened in update mode, and automatically deleted when the program ends
io.type(file): Check if obj is a usable file handle
io.flush(): Write all data in the buffer to the file
io.lines(optional file name) Return an iterative function, each call will get a line of content in the file, when it reaches the end of the file, it will return nil, but the file will not be closed

Full mode

  • Process multiple files at the same time. Need to use file:function_name instead of io.function_name method. The following example demonstrates how to process the same file at the same time:
-- 以只读方式打开文件
file = io.open("test.lua", "r")

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

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

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

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

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

-- test.lua 文件
  • The parameters of read are consistent with the simple mode.
  • Other methods:
file:seek(optional whence, optional offset)
  • Set and get the current file position. If it succeeds, it returns the final file position (in bytes). If it fails, it returns nil plus an error message. The parameter whyce value can be:

    • "set": start from the beginning of the file
    • "cur": start from the current position [default]
    • "end": start from the end of the file
    • offset: default is 0
    • Without parameter file:seek() returns the current position, file:seek("set") locates to the beginning of the file, file:seek("end") locates to the end of the file and returns the file size
file:flush()
  • Write all data in the buffer to the file
io.lines(optional file name)
  • :打开指定的文件filename为读模式并返回一个迭代函数,每次调用将获得文件中的一行内容,当到文件尾时,将返回nil,并自动关闭文件。
    若不带参数时io.lines() <=> io.input():lines(); 读取默认输入设备的内容,但结束时不关闭文件,如:

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

  print(line)

end

 

  • 以下实例使用了 seek 方法,定位到文件倒数第 25 个位置并使用 read 方法的 *a 参数,即从当期位置(倒数第 25 个位置)读取整个文件。
-- 以只读方式打开文件
file = io.open("test.lua", "r")

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

-- 关闭打开的文件
file:close()
</pre>
<p>我这边输出的结果是:</p>
<pre>
st.lua 文件末尾--test

【转载】原文链接:https://www.runoob.com/lua/lua-file-io.html

Guess you like

Origin blog.csdn.net/baidu_41388533/article/details/108504960