【问题标题】:I get unwanted data when I save a file with Rapidxml使用 Rapidxml 保存文件时收到不需要的数据
【发布时间】:2019-02-02 23:26:59
【问题描述】:

在开头创建一个声明节点和一个名为“List”的 elementNode 的简单函数,其中包含 26 个 elementNode,每个名为“IP”,以字符串数据类型的随机 IPv4 地址作为节点的值。随机 ip 来自一个包含所有 26 个 IP 的存储向量。 IP 和向量没有问题,因为它在控制台上正确输出。

为了确保在调用方法后节点仍然存在,我将它们存储在函数外部的向量中,即使我很确定它不会改变任何东西来做到这一点。

我尝试在循环内声明 ip 字符串并将值分配给循环内的变量,因为它可能会泄露信息。它不会改变任何东西。

另外,我也尝试用数字作为字符串来不同地命名每个 IP 节点。最糟糕的是它会输出纯垃圾。

我得到了一些提示,让我的问题变得更好,所以我编辑了代码。现在一切都很重要,仅此而已。

#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include <sstream>

#include <Dependencies/Rapidxml/rapidxml.hpp>
#include <Dependencies/Rapidxml/rapidxml_utils.hpp>
#include <Dependencies/Rapidxml/rapidxml_print.hpp>

#include "IPV4.h"

int8_t ComputerQty = 25;
std::vector<rapidxml::xml_node<>*> IPVec;
std::vector<IPV4> IPVector;
std::string FilePath = "C:/Users/Bob/Documents/Visual Studio 2017/Projects/UtryonWithIrrLicht/Files/IPList.xml";

int main(int argc, char** argv)
    {for (size_t A = 0; A < ComputerQty + 1; A++)
        {IPV4 NewIP = IPV4();
        NewIP.SetRandomIP();
        IPVector.push_back(NewIP);
        bool IpCollision = true;
        while (IpCollision)
            {IpCollision = false;
            size_t Size = IPVector.size();
            for(size_t B = 0; B < Size; B++)
                {if (A != B)
                        {if (IPVector[A].GetIP() == IPVector[B].GetIP())
                                {IpCollision = true;
                                if  (A < B)
                                        {IPVector[B].SetRandomIP();}
                                }
                        }
                }
            }
        }

    rapidxml::xml_document<> Doc;
    rapidxml::xml_node<>* Decl = Doc.allocate_node(rapidxml::node_declaration);
    Decl->append_attribute(Doc.allocate_attribute("version", "1.0"));
    Decl->append_attribute(Doc.allocate_attribute("encoding", "utf-8"));
    Doc.append_node(Decl);
    rapidxml::xml_node<>* List = Doc.allocate_node(rapidxml::node_element, "List");
    Doc.append_node(List);
    for (size_t A = 0; A < ComputerQty + 1; A++)
        {std::cout << "IPVector[A].GetIP() = " << IPVector[A].GetIP() << std::endl;
        IPVec.push_back(Doc.allocate_node(rapidxml::node_element, "IP", IPVector[A].GetIP().c_str()));
        List->append_node(IPVec[A]);
        }
    std::ofstream theFile(FilePath);
    theFile << Doc;
    theFile.close();
    Doc.clear();
    }

这是 Notepad++ 上的结果文件。

首先,这些奇怪的字符是什么,它们来自哪里? 从来没有见过这样的事情。 那么,为什么会有一些 IP 缺失呢? 最后为什么总是同一个IP?

【问题讨论】:

    标签: c++ xml file rapidxml


    【解决方案1】:

    我自己找到了答案。 事实证明,Node 构造函数不接受 std::string 尽管你可以这样做并且它不会在任何地方引发错误。它只会产生丑陋的输出。当您将 std::string 作为参数传递给构造函数时,您必须将其包装在 allocate_string(my_string) 函数中。似乎 rapidxml 以某种方式处理字符串并将其修改为所需的格式。我这样做了,输出很完美。

    我刚刚更改了那行:

    IPVec.push_back(Doc.allocate_node(rapidxml::node_element, 
                    "IP", 
                    IPVector[A].GetIP().c_str()));
    

    对于那一行:

    IPVec.push_back(Doc.allocate_node(rapidxml::node_element,  
                    "IP", 
                    Doc.allocate_string(IPVector[A].GetIP().c_str()));
    

    【讨论】:

    • 这是 stackoverflow 上几乎所有 RapidXML 问题的副本,但很好地找到了答案。需要明确的是,无论哪种情况,您都没有传递 std::string - 您在两种情况下都传递了 const char *,这就是没有错误的原因。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多