【发布时间】:2012-12-07 06:22:14
【问题描述】:
对不起,如果已经有这样的主题,但我找不到任何与 Lua 相关的内容......所以我在写入和读取文件时基本上遇到了一些问题,这就是我所做的:
hp = 25
file = io.open("player.txt","w")
if file==nil then
io.output("player.txt")
io.close()
end
file:write(hp)
file:close()
它似乎工作正常,它只是完美......但是当我尝试在 if 句中添加 file:write(hp) 时,它不起作用。另外,如果我在 file:write(hp) 之后添加 file:read("*line"),这就是它在 player.txt 中所说的:
25b[NUL]ÈñZ[NUL]
file = io.open("player.txt","w")
那么我做错了什么? [NUL] 也是记事本++中带有白色“NUL”文本的黑色块,但不能在此处复制。
编辑:嗯,好像整个代码都乱七八糟了,向上总是重写整个文件;o
Edit2:其实不知道我在说什么,现在我可以更多地理解文件控制,这是应该的或我尝试做的:
function existsFile(path)
x = io.open(path)
if x == nil then
io.close()
return false
else
x:close()
return true
end
end
if not existsFile("player.txt") then
file = io.open("player.txt", "w")
file:write(25)
hp = 25
file:close()
else
file = io.open("player.txt", "r")
hp = file:read("*number")
file:close()
end
而且我知道它看起来不像我第一次发布的代码,但这就是我的基本意思。
【问题讨论】: