lua io library (input and output)

Original Address http://www.freecls.com/a/2712/11

lua io The library provides two modes for file processing

1. Simple mode - all operations will be in 2 current files (default is stdin for standard input, stdout for standard output)
2. Full mode - based on file handle, a bit more versatile

Simple mode

--Receive any parameter and write to standard output by default stdout
io.write('www', '.','freecls','.','com','\n')

--Default from stdin read
local t = io.read('*n')
--'*a'  Read the entire file
--'*l'  Read next line(Exclude line breaks)=io.read()
--'*L'  Read next line(Include line breaks)
--'*n'  Read a number(Non-numeric Start Return nil,Number begins until a non-number is read)
--num   read num Characters

--Returning empty characters means you haven't reached the end of the file yet. Return nil Represents the end of the file
io.read(0)

io.input---io.input ([file])
--Open and set as default read file
--If provided is handler,Then set it directly
--If no parameters are provided,Return default read file

[io.output]---io.output ([file])
--Open and set default output file
--Other as above

--antic
local tmp = io.input()  --Save current input file handle
io.input('tmp.txt')     --Open a new file and set it as the current file handle
--do something
io.input():close()      --Close File
io.input(tmp)           --recovery


--Iterate line by line to read the entire file
io.lines()

--Example
local lines = {}
for line in io.lines() do lines[#lines + 1] = line end
--sort
table.sort(lines)
--Rewrite
for _, l in ipairs(lines) do io.write(l, "\n") end


Full mode

io.open--io.open(filename [,mode])

-- r	Open file read-only,The file must exist.
-- r+	Open Files Readable and Writable,Other Same
-- w	Open Write-Only File,Content Clear 0,Creation does not exist
-- w+	Read-write mode on,Other as above
-- a	Append Write-Only Mode Open,Create if not present
-- a+	Append Read-Write Mode Open,Other as above
-- b	binary mode,If the file is a binary file
-- +	Indicates that the file can be read or written

-- Open file read-only
local f = io.open("test.lua", "r+")

-- Read the first line
file:read()

-- Close open files
file:close()

-- Write content
file:write("--test")

--Locate file cursor(That is, setting where to read or write data from a file)
file:seek(whence, offset)
--"set": Start from file header
--"cur": Start from the current location[default]
--"end": Start at the end of the file
--offset:Default 0

file:seek()     --Return to current location
file:seek("set")    --Locate to File Header
file:seek("end")    --Navigate to end of file

file:flush()
--refresh buffer
--Generally for performance reasons, when writing a piece of data to a file, it is not written immediately, but first
--Cache to a memory buffer, and then when a condition is met or a display call is made file:flush()time
--Will write the buffer's data to the file immediately

file:setvbuf(mode[, size])
--Set Buffer Mode
-- 'no' Direct write without buffer(Not recommended unless in development debugging phase)
-- 'full' Full buffer, write immediately only when the buffer is full
-- 'line' Line buffer, written immediately when line breaks are encountered
-- When displaying calls file:flush()Will write immediately


io.popen (prog [, mode]) 

Run a program and return a file handle to read and write. Model defaults to r
Note that this method is not supported by every operating system
My name is centos7 64 bit which can be used to execute shell commands

Example

local h = io.popen('ls -al /')
local s = h:read("*a")
print(s)


io.type(obj)

Handle to detect whether obj is legal, return
'file'represents an open file handle
'closed file'represents a closed file handle
'nil'means not a file handle


io.tmpfile()

Opens a temporary file and returns the handle, which is automatically deleted at the end of the program


Summary

1. This article is just a brief introduction to io library, if you have any questions, you can leave a message for me
2.lua version 5.1, running environment centos7 64 bit
3. Original Address http://www.freecls.com/a/2712/11

Tags: shell

Posted on Mon, 09 Mar 2020 12:05:29 -0400 by Pi_Mastuh