Lua file read/write details
This article mainly introduces the Lua File Read and Write details. This article describes the simple and complete file read and write models and provides an operation example. For more information, see
The file read/write model in lua comes from the C language and is divided into a complete model (the same as C) and a simple model.
1. Simple Model
Io. input ([file]) sets the default input file. file is the file name (which will be read in text at this time) or file handle (which can be understood as the handle and the file can be found with the handle ), returns the file handle.
Io. output ([file]) sets the default output file. The parameter meaning is the same as above.
Io. close ([file]) closes the file, and closes the default file without Parameters
Io. read (formats) reads the default file. The value of formats is "* a" (read all), "* n" (read by number), and "* l" (read by row, by default), n (that is, a number, reads n characters ).
Io. lines ([fn]) fn file name. If no file exists, take the default file and return an iterator, which can be used in the for loop.
Io. write (value) writes content to the default file.
Io. flush () immediately applies the operations in the file cache to the default output file.
The example is at the end.
2. Complete Model
In a simple model, only files of the text type can be processed, and binary files can be processed in the complete model.
The general process for processing a file is to open the file and obtain the file handle, operate the file with the file handle, and close the file.
It can be seen that the complete model is more complex than the simple model, but the advantage is that it is more powerful.
Io. open (fn [, m]) open the file, return the file handle, fn file name, m mode:
R: open the file in read-only mode. The file must exist.
W. Open and write only the file. If the file exists, the file length is 0, indicating that the file content will disappear. If the file does not exist, the file is created.
A. Open and write-only files as an attachment. If the file does not exist, the file 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 reserved)
R + can open a file in read/write mode. The file must exist.
W + open the readable and writable file. If the file exists, the file length is cleared to zero, that is, the file content disappears. If the file does not exist, the file is created.
A + is similar to a, but this file is readable and writable.
B binary mode. If the file is a binary file, you can add B
+ Indicates that the file can be read or written.
The following file is the file handle returned by io. open, which is similar to the simple mode and is not described in detail.
File: close ()
File: read (formats)
File: lines ()
File: write (values)
File: seek ([p] [, of]) sets the file read/write offset. the starting position of the p file offset (value: "set", file header. This is the default value, "cur" current location, "end" file tail), of offset (default value 0, positive indicates forward, negative indicates backward), return the new current location in the file.
File: flush ()
3. Example
Copy the Code as follows:
------------------ Simple model -----------------
-- Read
Local file1 = io. input ("1.txt") -- the current directory" 1.txt" must exist. Otherwise, an error occurs.
Local str = io. read ("* ")
Print (str)
-- Write
Local file2 = io. output ("2.txt") -- the current directory" 2.txt" does not need to exist
Io. write (str)
Io. flush ()
Io. close ()
-- These functions can be used as a file copy function.
Function copy (fileA, fileB)
Local file1 = io. input (fileA)
If not file1 then print (fileA .. "does not exist") return end
Local str = io. read ("* ")
Local file2 = io. output (fileB)
Io. write (str)
Io. flush ()
Io. close ()
End
For line in io. lines ("1.txt") do
Print (line)
End
------------------ Complete model -----------------
Local f = io. open ("3.txt"," a + ")
F: write ("Happy New Year! ")
F: flush ()
F: seek ("end",-1) -- locate the first byte at the end of the file
Local str = f: read (1) -- read a character
Print (str) -- output "! "
F: close ()