我得到的无用数据时,我保存Rapidxml文件

问题描述 投票:-6回答:1

简单的函数来创建之初的声明节点,并命名为“列表”的elementNode,含26个elementNodes每个命名的“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();
    }

这里是记事本++生成的文件。 A list of IPs except most start and end with NUL, a few have b's at the end, and a few have digits missing from the end.

第一件事,第一,什么是论文奇怪的字符和他们来自哪里?从来没有见过这样的事情。那么,为什么有一些失踪的IP碎片?最后,为什么它总是相同的IP?

c++ xml file rapidxml
1个回答
0
投票

我自己找到了答案。事实证明,节点构造不接受的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()));
© www.soinside.com 2019 - 2024. All rights reserved.