Lua file reading and writing details

Time:2022-5-6

The file reading and writing model in Lua comes from C language and is divided into complete model (the same as C) and simple model.

1. Simple model

io. Input ([file]) sets the default input file. File is the file name (which will be read in as text at this time) or file handle (which can be understood as a handle. With a handle, you can find the file) and returns the file handle.
io. Output ([file]) sets the default output file. The meaning of the parameters 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, and the values of formats are “* a” (read all), “* n” (read by number), “* l” (read by line, the default method), n (i.e. number, read n characters).
io. Lines ([FN]) FN file name. If there is no file, 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

Only text files can be processed in the simple model, and binary files can be processed in the complete model.

The general process of processing files is: open the file and get the handle of the file; Operate the file with the file handle; Close the file.

It can be seen that the complete model is more complex than the simple model, but the advantage is more powerful.

io. Open (FN [, M]) opens the file and returns the file handle, FN file name, and M modes:
R to open a file as read-only, the file must exist.
W open the write only file. If the file exists, the file length will be cleared to 0, that is, the content of the file will disappear. If the file does not exist, the file is created.
A opens the write only file as an attachment. 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 character reserved)
R + to open a file in read-write mode, the file must exist.
W + open the read-write file. If the file exists, the file length will be cleared to zero, that is, the content of the file will disappear. 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, B can be added
+Sign indicates that the file can be read or written
The following file is Io The function of the file handle returned by open is similar to that of the simple mode and will not be described in detail.
file :close ()
file :read ( formats )
file :lines ()
file :write ( values )
File: seek ([P] [, of]) sets the offset of file reading and writing, the starting position of P file offset (the value has “set”, the file header, which is the default value, “cur” current position, “end” file tail), the of offset (the default value is 0, positive indicates forward, negative indicates backward), and returns the new current position in the file.
file :flush ()

3. Examples

Copy codeThe code is as follows:


——————Simple model—————–
–Read
local file1=io. Input (“1. TXT”) — the current directory “1. TXT” must exist, or an error will occur
local str=io.read(“*a”)
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()

 

–Using these functions, you can do 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(“*a”)
  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 previous byte at the end of the file
Local STR = f: read (1) — read one character
Print (STR) — output “!”
f:close()

 

Recommended Today

Processing of time series data in tensorflow window

Processing of tensorflow time series data Data set introduction Data source:Kaggle Ubiquant Market Prediction The dataset describes 300 anonymous features (“f_0” to “f_299”) and one target feature (“target”) of multiple investment projects in a time series. It is required to predict the target characteristics according to the anonymous characteristics of subsequent time nodes. The main goal […]