File I/O operation tutorial in Lua
This article mainly introduces the file I/O operation tutorial in Lua, which is the basic knowledge of Lua beginners. For more information, see
The I/O Library in Lua is used to read and process files. There are two types of file operations, namely, implicit file descriptor and clear file descriptor in Lua.
In the following example, we will use the example file test. lua, as shown in.
The Code is as follows:
-- Sample test. lua
-- Sample2 test. lua
Use the following statement to open a simple file.
The Code is as follows:
File = io. open (filename [, mode])
Various file modes are listed in the following table.
Hidden file descriptor
The hidden file descriptor uses the standard input/output mode, or a single input/output file. An example of using the descriptor of an implicit file is as follows.
The Code is as follows:
-- Opens a file in read
File = io. open ("test. lua", "r ")
-- Sets the default input file as test. lua
Io. input (file)
-- Prints the first line of the file
Print (io. read ())
-- Closes the open file
Io. close (file)
-- Opens a file in append mode
File = io. open ("test. lua", "")
-- Sets the default output file as test. lua
Io. output (file)
-- Appends a word test to the last line of the file
Io. write ("-- End of the test. lua file ")
-- Closes the open file
Io. close (file)
When you run the program, the first line of output of the test. lua file is obtained. In this example, the following output is obtained.
The Code is as follows:
-- Sample test. lua
This is the first line to declare the test. lua file. "-- End of the test. lua file" will be appended to the last line of the test. lua code.
In the above example, we can see how the Implicit description works with the file system io. "×" method. The preceding example uses io. read () without any optional parameters. The optional parameter can be any of the following.
Other common IO methods include:
Io. tmpfile (): returns the read/write temporary file. Once the program exits, the file will be deleted.
Io. type (file): returns a file, closes the file, or returns the input file.
Io. flush (): clears the default output buffer.
Io. lines (optional file name): provides a general loop iterator to traverse the file and close the file that provides the file name and default file in the final case, it is not closed at the end of the loop.
Clear file descriptor
We often use clear file descriptors so that we can process multiple files at the same time. These functions are similar to implicit file descriptors. Here, we use the file: function name, instead of io. function_name. The following example shows the version of the same implicit file descriptor.
The Code is as follows:
-- Opens a file in read mode
File = io. open ("test. lua", "r ")
-- Prints the first line of the file
Print (file: read ())
-- Closes the opened file
File: close ()
-- Opens a file in append mode
File = io. open ("test. lua", "")
-- Appends a word test to the last line of the file
File: write ("-- test ")
-- Closes the open file
File: close ()
When you run the program, the example that will be implicitly described is similar output.
The Code is as follows:
-- Sample test. lua
All the modes in which the file is opened and the parameter is used to read the external description are the same as the descriptor of the hidden file.
Other common file methods include:
File: seek (optional whence, optional offset): parameter "set", "cur" or "end ". Set the position of the file where the new file pointer is updated from the beginning of the file. The offset is zero-based. From the offset measured when the first parameter is "set" at the beginning of the file, from the current location in the "cur" file, or from the end of the file, for "end ". The default parameter values are "cur" and 0. Therefore, the current file location can be obtained by calling the function without parameters.
File: flush (): clears the default output buffer.
Io. lines (optional file name): provides a general loop iterator to traverse the file and close the file that provides the file name and default file in the final case, it is not closed at the end of the loop.
For example, the following method is used. Offsets starts from the end of the file before the cursor at 25 positions. Print the remaining seek position from the file reading function.
The Code is as follows:
-- Opens a file in read
File = io. open ("test. lua", "r ")
File: seek ("end",-25)
Print (file: read ("* "))
-- Closes the opened file
File: close ()
The output is similar to the following.
The Code is as follows:
Sample2 test. lua
-- Test
You can use different modes and parameters to understand the Lua file operation capabilities.