簡單 I/O 模型
- io.input(filename)
- io.output()
- io.write
print() 與 io.write
print 中 "," 會轉(zhuǎn)為 制表符 ,會自動在末尾加 換行符 - io.read
"all" 讀取整個文件;
"line"讀取下一行;
"*number"讀取一個數(shù)字;
<num>讀取一個不超過<num>個字符的字符串
MIME quoted-printable 編碼方式 : 會將 acs 碼值 轉(zhuǎn)化為 =<xx> - io.lines
實例代碼
io.write( "sin(3) = " , math.sin(3) , "\n") --> sin(3) = 0.14112000805987
io.write( string.format("sin(3)=%.4f\n", math.sin(3) ) ) --> sin(3)=0.1411
-- io.write(a..b..c) io.write(a,b,c)
print("hello" , "Lua" );print("Hi")
io.write("hello" , "Lua" );io.write("Hi")
-- t = io.read("*all") -- 讀取整個文件
-- t = string.gsub( t , ... ) -- 做相關(guān)的處理
-- io.write(t) -- 寫輸出
--[[
t = io.read("*all")
t = string.gsub( t , "(\128-\255=])" , function(c)
return string.format( "=%2x" , string.byte(c) )
end)
io.write(t)
--]]
--[[
for count = 1 , math.huge do
local line = io.read()
if line == nil then break end
io.write( string.format( "%6d " , count ) , line , "\n" )
end
--]]
--[[
local lines ={}
-- 讀取table 'lines' 中的所有行
for line in io.lines() do lines[#lines+1] = line end
table.sort(lines)
-- 輸出所有行
for _,l in ipairs(lines) do io.write( l , "\n" ) end
--]]
--[[
while true do
local n1 , n2 , n3 = io.read("*number" , "*number" , "*number")
if not n1 then break end
print("最大的數(shù):" , math.max(n1 , n2 , n3 ))
end
--]]
while true do
local block = io.read( 2^13 ) -- 緩沖大小為8k
if not block then break end
io.write(block)
end
io.read( 0 ) -- 用于檢查是否到達(dá)文件末尾 ,
-- 如果還有數(shù)據(jù)可以讀取的話,會返回 空字符串
-- 否則返回nil