Q:什么是”Simple Model”?

A:全部的文件操作都基于一个默认的输入文件和一个默认的输出文件。这就意味着同一时间对于输入和输出来说,仅仅可操作一个文件(默认的文件)。

默认的输入文件初始化是stdin,默认的输出文件初始化是stdout

-- "a.lua"文件里:
--[[ "io.read()"从"io.input()"所指定的默认输入文件里读;
"io.write()"向"io.output()"所指定的默认输出文件里写。 由于默认输入文件初始化是"stdin";默认的输出文件初始化是"stdout",
所以"io.read()"从"stdin"读;"io.write()"向"stdout"写。]]
io.write(io.read())
--[[ result:
> lua a.lua
Hello World!
Hello World!]]

io.read()io.write()都能够接收多个參数。

-- "a.lua"文件里:
io.write(io.read(), " and", " The Complete I/O Model.")
--[[ result:
> lua a.lua
The Simple I/O Model
The Simple I/O Model and The Complete I/O Model.]]

io.input()io.output()能够更改相应的默认文件。

-- "file1.txt"文件里:
Hello World!
-- "a.lua"文件里:
local temp_input = io.input() -- 保存原先的默认输入文件(此时是"stdin")。
local temp_output = io.output() -- 保存原先的默认输出文件(此时是"stdout")。
io.input("file1.txt") -- 指定默认的输入文件为"file1.txt"。
io.output("file2.txt") -- 指定默认的输出文件为"file2.txt"。
io.write(io.read())
io.input():close() -- 关闭"file1.txt"。
io.output():close() -- 关闭"file2.txt"。
io.input(temp_input) -- 恢复原先的默认输入文件。 io.output(temp_output) -- 恢复原先的默认输出文件。
--[[ result:
> lua a.lua
> cat file2.txt
Hello World!]]

Q:print()io.write()有什么差别?

A:

1、print()在打印其每一个參数之间添加\t,且最后添加\n。而io.write()均不会。

print("hello", "Lua")
print("Hi")
--[[ result:
hello Lua
Hi]]
io.write("hello", "Lua")
io.write("Hi", "\n")
--> helloLuaHi

2、io.write()io.output()指定的文件里写(初始值是stdout)。而print()总向stdout写。

-- "a.lua"文件里:
local temp = io.output()
io.output("file3.txt")
io.write("Hello")
print(" World")
io.output():close()
io.output(temp)
--[[ result:
"file3.txt"文件里:
Hello
"stdout":
World]]

3、print()对于不能被直接打印的对象会自己主动调用tostring()转换。而io.write()不会。

t = {}
print(t) --> table: 0x1ca9aa0
io.write(t) --> error

Q:io.read()的參数?

A:

-- "file1.txt"文件里:
line1
line2
line3 -- "data.txt"文件里:
6.0 -3.23 15e12
4.3 234 1000001

1、*a从当前的文件读取位置读取到文件尾,假设文件为空或者已在文件尾,则返回空串,

local temp = io.input()
io.input("file1.txt")
io.write(io.read("*a"))
io.input():close()
io.input(temp)
--[[ result:
line1
line2
line3]]

2、*l从当前的文件读取位置读取到行尾,不包括结尾的\n*L*l功能同样,但包括行尾的\n。假设文件为空或者已在文件尾,则返回nil

local temp = io.input()
io.input("file1.txt")
str = ""
local i = 1
while true do
local s
if i == 2 then
s = io.read("*L")
else
s = io.read("*l") -- "io.read()"不指定參数时。默认使用"*l"參数。
end
if s == nil then break end -- 当文件读取完时,返回"nil"。 str = str .. s
i = i + 1
end
io.write(str)
--[[ result:
line1line2
line3]]
io.input():close()
io.input(temp)

3、*n从当前的文件读取位置读取一个数值。io.read("*n")返回数值,而不是字符串。

当须要从一个文件里读取大量数字时,数字间的字符串为空格能够显著提高执行性能。io.read("*n")会跳过两个可被识别数字之间的随意空格。假设在当前位置找不到一个数字(由于格式不正确,或者是到了文件的结尾)。则返回nil

-- 打印文件每一行中最大的数字。
local temp = io.input()
io.input("data.txt")
while true do
local n1, n2, n3 = io.read("*n", "*n", "*n")
if not n1 then break end
print(math.max(n1, n2, n3))
end
io.input():close()
io.input(temp)
--[[ result:
15000000000000.0
1000001]]

4、io.read(num)能够指定一个数字,lua将尽可能读取”num”个字符(假设不够则能读多少读多少),假设文件为空或者已读取到文件尾则返回nil

local temp = io.input()
io.input("data.txt")
io.write(io.read(3)) -- 读取3个字符。
print()
-- 读取100个字符。 但实际没有那么多。所以能读取多少读多少。
io.write(io.read(100))
io.input():close()
io.input(temp)
--[[ result:
6.0
-3.23 15e12
4.3 234 1000001]]

Q:其它的”Simple Model”模式的库函数?

A:

--[[ io.tmpfile()
以"w+"的模式打开一个暂时文件,并返回其文件句柄。
当程序执行结束时,该文件会被自己主动删除。 ]]
local f = io.tmpfile()
f:write("123")
f:seek("set", 0) -- 将文件的当前读取位置定位到文件头。
print(f:read()) -- 123
f:close() --[[ io.type(obj)
检查"obj"是否为有效的文件句柄。 函数返回例如以下:
"file": 假设"obj"是一个被打开的文件句柄。
"closed file": 假设"obj"是一个被关闭的文件句柄。
nil: 假设"obj"不是一个有效的文件句柄。 ]]
print(io.type(f)) -- nil
f = io.tmpfile()
print(io.type(f)) -- file
f:close()
print(io.type(f)) -- closed file

附加:

1、io.input()io.output()在出错时均会报错。假设想控制错误。请使用”Complete model”下的io.open()

2、向io.write()传递的数字參数会被自己主动转换为字符串。

假设想全然控制转换,最好使用string.format()

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

3、避免写io.write(a..b..c)这种代码。io.write(a, b, c)能够完成同样的工作,同一时候避免了..操作符复制字符串所耗费的内存资源。

4、当文件为空,或者文件当前的读取位置在文件尾时,io.read("*a")会返回空字符串,而io.read("*l")io.read("*L")io.read("*n")io.read(num)都会返回nil

5、假设想要逐行的读取文件,相比io.read("*l")。更好的选择是io.lines()io.lines()能够指定想要读取的文件。而且能够指定多个,依次的按行读取这些文件。

同一时候在读取完成后。io.lines()会自己主动关闭文件。

-- 为文件的每一行添加行号。
-- "file1.txt"文件里。
line1
line2
line3
-- "a.lua"文件里。 local count = 1
for line in io.lines("file1.txt") do
io.write(string.format("%6d ", count), line, "\n")
count = count + 1
end
--[[ result:
> lua a.lua
1 line1
2 line2
3 line3]]

然而假设io.lines()不带參数,则与io.read()的功能同样,同一时候在读取完成后不会关闭文件。

6、假设对于文件须要逐块的操作。但终于会操作整个文件。

那么最好使用io.read("*a")读取整个文件,然后使用string.gmatch()分解出须要操作的块。

-- 上面打印文件每一行中最大的数字的样例。
-- 须要处理的块。一个或多个非空格加上一个或多个空格……
local block = "(%S+)%s+(%S+)%s+(%S+)%s+"
--[[ "string.gmatch()"返回的结果是字符串形式的,
假设直接传递给"math.max()"会依照字符串形式比較大小,
所以须要使用"tonumber()"转换为数字。]]
for n1, n2, n3 in string.gmatch(io.read("*a"), block) do
print(math.max(tonumber(n1), tonumber(n2), tonumber(n3)))
end

省去了多次读文件的操作。效率会更高。

7、在Lua中复制文件的高效的方式,

local size = 2^13    -- good buffer size (8K)
while true do
local block = io.read(size)
if not block then break end
io.write(block)
end

8、io.read(0)測试是否到了文件尾。假设未到,则返回空字符串,假设到了则返回nil

高速掌握Lua 5.3 —— I/O库 (1)的更多相关文章

  1. 高速掌握Lua 5.3 —— 字符串库 (2)

    Q:模式匹配字符串的相关函数? A: --[[ string.find(s, pattern [, init [, plain]]) 在字符串"s"中查找第一个与匹配模式" ...

  2. Lua的string和string库总结

    Lua有7种数据类型,分别是nil.boolean.number.string.table.function.userdata.这里我总结一下Lua的string类型和string库,复习一下,以便加 ...

  3. Lua中的常用函数库汇总

    lua库函数 这些函数都是Lua编程语言的一部分, 点击这里了解更多. assert(value) - 检查一个值是否为非nil, 若不是则(如果在wow.exe打开调试命令)显示对话框以及输出错误调 ...

  4. Lua中的字符串函数库

    字符串库中的一些函数是非常简单的: string.len(s)          返回字符串s的长度:string.rep(s, n)      返回重复n次字符串s的串:你使用string.rep( ...

  5. 高速掌握Lua 5.3 —— Lua与C之间的交互概览

    Q:什么是Lua的虚拟栈? A:C与Lua之间通信关键内容在于一个虚拟的栈.差点儿全部的调用都是对栈上的值进行操作,全部C与Lua之间的数据交换也都通过这个栈来完毕.另外,你也能够使用栈来保存暂时变量 ...

  6. 高速掌握Lua 5.3 —— 扩展你的程序 (1)

    Q:怎样在C中将Lua作为配置文件语言使用? A: "config.lua"文件里: -- window size width = 200 height = 300 "m ...

  7. Lua中的table函数库

    table.concat(table, sep,  start, end) concat是concatenate(连锁, 连接)的缩写. table.concat()函数列出参数中指定table的数组 ...

  8. C++ Primer高速入门之五:有用的模板库

    更新:又一次排版代码格式 除上篇博客介绍的基本数据类型外,C++ 还定义了一个内容丰富的抽象数据类 型标准库. 包含 string 和 vector,它们分别定义了字符串和矢量(集合).string ...

  9. Git高速入门——Git安装、创建版本号库以及经常使用命令

    学习Git最全面的资料,在我看来是这本书--Pro Git,网上关于Git的教程有非常多,包含当中一些非常优秀的教程.这一系列的博客,主要是记录自己学习Git的经历.以及在这一过程中遇到的一些问题. ...

随机推荐

  1. 高级聚合函数rollup(),cube(),grouping sets()

       rollup(),cube(),grouping sets()   上面这几个函数,是对group by分组功能做的功能扩展. a.rollup()   功能:在原结果基础上追加一行总合计记录 ...

  2. 工具-VS使用GIT工具

    由于VS中集成了GIT插件,本机安装了GIT工具和TortoiseGit工具,造成在VS中GIT无法同步,于是将TortoiseGit卸载,再次启用VS中的GIT插件,重新初始化GIT文件夹,问题解决 ...

  3. 关于nodejs的线程模型可以看这篇文章

    虽然还是有一些没有讲全,但是整体还是讲的很不错的. http://www.ruanyifeng.com/blog/2014/10/event-loop.html

  4. storm-安装

            storm有两种操作模式: 本地模式和远程模式.使用本地模式的时候,你能够在你的本地机器上开发測试你的topology, 一切都在你的本地机器上模拟出来; 用远端模式的时候你提交的to ...

  5. 玩转Android Camera开发(三):国内首发---使用GLSurfaceView预览Camera 基础拍照demo

    GLSurfaceView是OpenGL中的一个类,也是能够预览Camera的,并且在预览Camera上有其独到之处. 独到之处在哪?当使用Surfaceview无能为力.痛不欲生时就仅仅有使用GLS ...

  6. php中echo什么时候用到\"这个符号

    php中echo什么时候用到\"这个符号 当在引号中用到引号时,为避免混乱用\" \称为转义符,表示后面的字符和原来程序语言里的语法符号含义不同 常见的转义有 \" \' ...

  7. CentOS7安装EPEL的两种方式

    转自:http://www.mamicode.com/info-detail-1671603.html epel是社区强烈打造的免费开源发行软件包版本库. EPEL,即Extra Packages f ...

  8. 浏览器输入一个url的过程,以及加载完html文件和js文件的标志

    简单理解: 当在浏览器地址栏输入一url时,浏览器会做以下几个步骤: 1.将url转化为ip地址,也就是DNS解析,(先找本地host文件中是否有对应的ip地址,如果有就直接用,没有的话,就按域名的二 ...

  9. direct2d: antialiasing and drawing a line with a 1 pixel stroke

    http://xboxforums.create.msdn.com/forums/t/61448.aspx I'm currently porting a number of custom MFC C ...

  10. ACM-ICPC 2018 徐州赛区网络预赛 H. Ryuji doesn't want to study(树状数组)

    Output For each question, output one line with one integer represent the answer. 样例输入 5 3 1 2 3 4 5 ...