【问题标题】:tinyxml2 create file using printertinyxml2 使用打印机创建文件
【发布时间】:2015-02-18 18:38:33
【问题描述】:

我正在尝试使用 C++ 和 tinyxml2 制作一个 xml 文件,以保存游戏状态。现在我只是得到一个空文件,我不知道为什么。任何帮助,将不胜感激。

FILE * pFile;
pFile = fopen ("test.xml","w");
XMLPrinter printer(pFile);
printer.OpenElement("Rooms");
printer.OpenElement("room");
printer.PushAttribute("id","RID_DUNGEON_1");
printer.PushAttribute("description","unpleasant dungeon");
printer.PushAttribute("name","dungeon");
printer.OpenElement("items");
printer.OpenElement("item");
printer.PushAttribute("id","torch");
printer.CloseElement();
printer.CloseElement();
printer.CloseElement();
printer.CloseElement();

最终结果应该是这样的:

<Rooms>
    <room id="RID_DUNGEON_1" description="unpleasant dungeon" name="dungeon">
        <items>
            <item id="torch"/>
        </items>
    </room>
</Rooms>

【问题讨论】:

  • 我尝试了您的代码,它按您的预期工作。您应该检查 fopen 是否成功,您应该使用 fclose(pFile) 关闭文件以刷新。
  • 添加 fclose(pFile);到代码的末尾修复它。非常感谢!

标签: xml save tinyxml2


【解决方案1】:

写入 FILE* 不会写入磁盘,而是写入用户空间缓冲区。

为了刷新用户空间缓冲区,您应该调用 fflush(或调用 fflush 的 fclose)。

如果您想确保将数据写入存储设备,您应该在 fclose 之前调用fsync

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-17
    相关资源
    最近更新 更多