重新打开已关闭的文件:Lua

我有一个名为 backup.lua 的文件,程序应该每隔一段时间写入该文件以备份其状态,以防失败。

问题在于程序第一次很好地写入了 backup.lua 文件,但其他任何时间它都拒绝写入该文件。

我尝试在程序仍然打开时删除该文件,但 Windows 告诉我该文件正被“CrysisWarsDedicatedServer.exe”使用。我已告诉主机 Lua 函数关闭 backup.lua 文件,那么在文件关闭后为什么不让我随意修改文件呢?

我在互联网上找不到任何信息(Google 实际上试图更正我的搜索),项目的辅助程序员也不知道。

所以我想知道你们中是否有人知道我们在这里做错了什么?

主机函数代码:

function ServerBackup(要做的事)
local write,read;
if todo ==“write” then
    写=真;
否则
    读=真;
结尾
如果(写)则
    本地源=io.open(Root()..”Mods/Infinity/System/Read/backup.lua”,“w”);
    System.Log(TeamInstantAction:GetTeamScore(2).." for 2, and for 1: "..TeamInstantAction:GetTeamScore(1))
    System.LogAlways("[System] Backing up serverdata to file 'backup.lua'");
    source:write("--[[ The server is dependent on this file; editing it will lead to serious problems.If there is a problem with this file, please re-write it by accessing the backup system ingame.--]]");
    source:write(“备份={}”;备份.Time = “"..os.date("%H:%M").."';Backup.Date = '"..os.date("%d/%m/%Y").."';” );
    source:write(XFormat("TeamInstantAction:SetTeamScore(2, %d);TeamInstantAction:SetTeamScore(1, %d);TeamInstantAction:UpdateScores();",TeamInstantAction:GetTeamScore(2), TeamInstantAction:GetTeamScore(1) ));
    source:close();
    对于 i,玩家对 g_gameRules.game:GetPlayers()或 {} 进行遍历 do
        如果(是主持人(玩家))则
            CMPlayer(player,“[!backup] Completed server backup.”);
        结尾
    结尾
结尾
--local source = io.open(Root().."Mods/Infinity/System/Read/backup.lua", "r"); 脚本加载器不会也打开文件吗?
如果(阅读)则
    System.LogAlways("[System] Restoring serverdata from file 'backup.lua'");
    -- source:close();
    备份={};
    Script.LoadScript(Root()..”Mods/Infinity/System/Read/backup.lua”);
    如果备份或#备份<1则
        System.LogAlways("[System] Error restoring serverdata from file 'backup.lua'");
    结尾
结尾
end

感谢大家:)。

_ 编辑:_

虽然该文件现在已成功写入磁盘,但系统无法读取转储的文件。

点赞
用户1737
用户1737

因此,现在的问题是,“LoadScript”函数没有达到你的预期:

因为我是心灵感应的,我猜测你正在编写一个Crysis插件,并且正在尝试使用它的LoadScript API调用

(请不要以为这里的每个人都会猜到这一点或者会去寻找这个信息。这是必须作为你问题的一部分的重要信息)

你编写的脚本尝试设置“备份”(Backup)-但是你编写的脚本没有使用换行符分隔行。由于第一行是注释,整个脚本将被忽略。

基本上,你编写的脚本看起来像这个样子,这些都被视为注释。

 - -[[ comment ]]--Backup="Hello!"

你需要在注释后面写上一个"\n"(在其他地方也建议这样做)以得到如下结果。实际上,你甚至不需要使用块注释。

--  comment
Backup="Hello!"
2013-07-15 10:25:07