Read/Write 来自文件的高分(Lua - Corona SDK)
Read/Write highscore from a file (Lua - Corona SDK)
这是我的问题:我有一个文件,里面写着最高分(只有第一行,没有昵称,只有最高分),我需要阅读那一行并将其与游戏中获得的实际分数进行比较session,如果分数更高,用新值覆盖文件,但如果我尝试读取它,我得到一个空值……好像我没有以正确的方式读取它。我的代码有什么问题?
感谢您的帮助!
local path = system.pathForFile( "data.sav", system.DocumentsDirectory )
local file = io.open( path, "w+" )
highscore_letta = file:read("*n")
print(highscore_letta)
if (_G.player_score > tonumber(highscore_letta)) then
file:write(_G.player_score)
end
io.close( file )
我自己也遇到过这个问题。我发现如果你以 "w+"
模式打开文件,当前内容会被删除,这样你就可以 写入 新内容。所以要读写你必须打开文件两次。首先,您以 "rb"
模式打开文件并获取文件内容,然后关闭它。然后以 "wb"
模式重新打开它,写入新号码,然后关闭它。
在Windows中,你需要在文件模式下"b"
。否则,您正在读取和写入的字符串可能会以意想不到的方式被修改:例如,换行符 ("\n"
) 可能会替换为回车符 return–换行符 ("\r\n"
)。
Lua支持的文件模式是从C语言借来的。 (我在第 305 页找到了描述,我猜是 a draft of the C specification。)我认为 Lua 手册假设您会像有经验的 C 程序员一样知道这些模式的含义,但是我一点也不明显。
因此读取一个数字然后写入一个新数字:
local filepath = "path/to/file"
-- Create a file handle that will allow you to read the current contents.
local read_file = io.open(filepath, "rb")
number = read_file:read "*n" -- Read one number. In Lua 5.3, use "n"; the asterisk is not needed.
read_file:close() -- Close the file handle.
local new_number = 0 -- Replace this with the number you actually want to write.
-- Create a file handle that allows you to write new contents to the file,
-- while deleting the current contents.
write_file = io.open(filepath, "wb")
write_file:write(new_number) -- Overwrite the entire contents of the file.
write_file:flush() -- Make sure the new contents are actually saved.
write_file:close() -- Close the file handle.
我创建了一个脚本来自动执行这些操作,因为每次输入它们有点烦人。
"r+"
或 "r+b"
模式应该允许您读取 和 写入,但我无法在原来的情况下使用它内容比新内容长。如果原来的内容是"abcd"
,四个字节,新内容是"efg"
,三个字节,你在文件中的偏移量0
处写入,文件现在将有"efgd"
:原内容的最后一个字节不删除。
这是我的问题:我有一个文件,里面写着最高分(只有第一行,没有昵称,只有最高分),我需要阅读那一行并将其与游戏中获得的实际分数进行比较session,如果分数更高,用新值覆盖文件,但如果我尝试读取它,我得到一个空值……好像我没有以正确的方式读取它。我的代码有什么问题?
感谢您的帮助!
local path = system.pathForFile( "data.sav", system.DocumentsDirectory )
local file = io.open( path, "w+" )
highscore_letta = file:read("*n")
print(highscore_letta)
if (_G.player_score > tonumber(highscore_letta)) then
file:write(_G.player_score)
end
io.close( file )
我自己也遇到过这个问题。我发现如果你以 "w+"
模式打开文件,当前内容会被删除,这样你就可以 写入 新内容。所以要读写你必须打开文件两次。首先,您以 "rb"
模式打开文件并获取文件内容,然后关闭它。然后以 "wb"
模式重新打开它,写入新号码,然后关闭它。
在Windows中,你需要在文件模式下"b"
。否则,您正在读取和写入的字符串可能会以意想不到的方式被修改:例如,换行符 ("\n"
) 可能会替换为回车符 return–换行符 ("\r\n"
)。
Lua支持的文件模式是从C语言借来的。 (我在第 305 页找到了描述,我猜是 a draft of the C specification。)我认为 Lua 手册假设您会像有经验的 C 程序员一样知道这些模式的含义,但是我一点也不明显。
因此读取一个数字然后写入一个新数字:
local filepath = "path/to/file"
-- Create a file handle that will allow you to read the current contents.
local read_file = io.open(filepath, "rb")
number = read_file:read "*n" -- Read one number. In Lua 5.3, use "n"; the asterisk is not needed.
read_file:close() -- Close the file handle.
local new_number = 0 -- Replace this with the number you actually want to write.
-- Create a file handle that allows you to write new contents to the file,
-- while deleting the current contents.
write_file = io.open(filepath, "wb")
write_file:write(new_number) -- Overwrite the entire contents of the file.
write_file:flush() -- Make sure the new contents are actually saved.
write_file:close() -- Close the file handle.
我创建了一个脚本来自动执行这些操作,因为每次输入它们有点烦人。
"r+"
或 "r+b"
模式应该允许您读取 和 写入,但我无法在原来的情况下使用它内容比新内容长。如果原来的内容是"abcd"
,四个字节,新内容是"efg"
,三个字节,你在文件中的偏移量0
处写入,文件现在将有"efgd"
:原内容的最后一个字节不删除。