May 12, 2021 Lua
The Lua I/O library is used to read and process files. Divided into simple mode (as C), full mode.
Simple mode is more appropriate when doing some simple file operations. B ut when it's some advanced file operation, simple mode can't help. F or example, to read multiple files at the same time, it is more appropriate to use full mode.
The opening file action statement is as follows:
file = io.open (filename [, mode])
The values of mode are:
Mode | Describe |
---|---|
R | Open the file in a read-only manner, and the file must exist. |
W | Open a write-only file, and if the file exists, the file length is clear to 0, i.e. the contents of the file disappear. If the file does not exist, establish the file. |
A | Open write-only files in an additional way. I f the file does not exist, the file is established, and if the file exists, the written data is added to the end of the file, i.e. the original contents of the file are preserved. (EOF character reserved) |
r+ | Open the file as readable and write, and the file must exist. |
w+ | Open the readable and write file, and if the file exists, the file length is zero, that is, the contents of the file disappear. If the file does not exist, establish the file. |
a+ | Similar to a, but this file is readable and writeable |
B | Binary mode, if the file is a binary, you can add b |
+ | The number indicates that the file can be read or written |
Simple mode uses standard I/O or uses a current input file and a current output file.
Here's the file .lua file code, and the file you're .lua (if you don't need to create the file), 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)
By executing the above code, you will find that the first test.ua of the file is output and a comment for lua is added to the last line of the file. As my side outputs:
-- test.lua 文件
In the above example we used io." The x" method, where io.read() does not have parameters, which can be one of the following tables:
Mode | Describe |
---|---|
"*n" | Read a number and return it. Example: file.read ("n") |
"*a" | Read the entire file from its current location. 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 that specifies the number of characters, or nil at EOF. Example: file.read (5) |
Other io methods are:
io.tmpfile(): Returns a temporary file handle that opens in update mode and is automatically deleted at the end of the program
io.type (file): Detect whether obj is an available file handle
io.flush(): Write all the data in the buffer to the file
io.lines (optional file name): Returns an iterative function, each call will get a line in the file, and when it comes to the end of the file, it will return nil, but not the file
Usually we need to process multiple files at the same time. W e need to use file:function_name instead of io.function_name method. The following example demonstrates the same file as being processed 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()
By executing the above code, you will find that the first test.ua of the file is output and a comment for lua is added to the last line of the file. As my side outputs:
-- test.lua 文件
Read's parameters are consistent with the simple pattern.
Other methods:
file:option whence, optional offset: set and get the current file location, successfully return the final file location (by byte), failure returns nil plus error message. The parameter whence value can be:
file:flush(): Write all the data in the buffer to the file
io.lines (optional file name):
Open the specified file file for read mode and return an iterative function, each call will get a line of content in the file, when the end of the file, will return nil, and automatically close the file.
If you do not have parameters io.lines () io.input(): lines ();
Read the contents of the default input device, but do not close the file at the end, such as
for line in io.lines("main.lua") do print(line) end
The following example uses the seek method to locate the 25th position of the file and to read the entire file from the current position (the penultimate position) using the s a parameter of the read method.
-- 以只读方式打开文件 file = io.open("test.lua", "r") file:seek("end",-25) print(file:read("*a")) -- 关闭打开的文件 file:close()
The output on my side is:
st.lua 文件末尾--test