Lua文件读写错误

发布于 2024-11-08 00:18:46 字数 1082 浏览 3 评论 0原文

抱歉,如果已经有这样的主题,但我找不到任何与 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

编辑2:实际上不知道我在说什么,现在我可以了解更多的文件控制,这就是它应该是什么或我试图做的:

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

我知道它看起来不像我第一次发布的代码,但这就是我的基本意思。

Sorry if there's already a topic like this, but I couldn't find any that have something to do with Lua... So I'm basically having some problems in writing and reading files, here's what I've done:

hp = 25

file = io.open("player.txt","w")
if file==nil then
    io.output("player.txt")
    io.close()
end
file:write(hp)
file:close()

and it seems to work fine, it's just perfect... but then when I'm trying to add the file:write(hp) inside the if-sentence, it doesn't work. Also if I'll add file:read("*line") right after file:write(hp), this is what it says in player.txt:

25b[NUL]ÈñZ[NUL]
file = io.open("player.txt","w")

So what am I doing wrong? Also [NUL] is black block with white "NUL" text in it in notepad++ but it can't be copied here.

Edit: Hmmh, seems like the whole code is messed, up it always rewrites the whole file ;o

Edit2: Had actually no idea what I was talking about, nowadays I can understand file controlling bit more, here's what it should've been or what I tried to do:

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

And I know it doest look anything like the code I first posted, but that's what I basically meant.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

洒一地阳光 2024-11-15 00:18:46

你能解释一下你想在这段代码中做什么吗?
为什么需要检查文件是否为零?当你打开文件进行写入时,如果不存在,lua 会自动创建它。
“w”模式意味着您将删除文件中的所有数据并写入新数据
也许您需要“a”模式?在此模式下,新行将添加到文件末尾。

Could you explain what you are trying to do in this code?
Why do you need to check if file is nil? When you open file for writing, lua automatically creates it if not exists.
"w" mode means, that you you're erase all data in file and write new data
May be you need "a" mode? In this mode new lines are added at the end of file.

醉殇 2024-11-15 00:18:46

听起来您对 io.open。检查手册以确保您真正想要的是 w 标志,因为它会覆盖所有内容。

当您处于 if 状态时尝试执行 file:write 应该不起作用,而且我不确定您为什么会期望它这样做,因为 文件nil。您是说,如果无法打开文件,则尝试将其写入文件,这对我来说没有意义。

Sounds like you're confused about the flags on io.open. Check the manual to be sure what you really want is the w flag since that overwrites everything.

Trying to do a file:write when you're in the if shouldn't work, and I'm not sure why you'd expect it to, since file is nil. You're saying that if the file couldn't be opened, then try to write this to the file, which doesn't make sense to me.

欢你一世 2024-11-15 00:18:46

“if”块检查“file”是否为零,以便该代码块永远不会运行。

read() 不起作用,因为您以“w”(写入)模式打开文件。

擦除整个文件是写入模式的预期行为。在该模式下,文件首先被删除,然后向其中写入新数据。

The "if" block checks if "file" is nil, so that code block will never run.

read() doesn't work because you opened the file in "w" (write) mode.

Erasing the whole file is the expected behavior of write mode. In that mode the file is first erased and then you write new data to it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文