Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用Rapidxml保存文件时,会收到不需要的数据_C++_Xml_File_Rapidxml - Fatal编程技术网

C++ 使用Rapidxml保存文件时,会收到不需要的数据

C++ 使用Rapidxml保存文件时,会收到不需要的数据,c++,xml,file,rapidxml,C++,Xml,File,Rapidxml,一个简单的函数,用于在开头创建一个声明节点和一个名为“List”的elementNode,其中包含26个elementNodes,每个elementNodes都名为“IP”,其中一个字符串数据类型中的随机IPv4地址作为节点的值。随机ip来自包含所有26个ip的存储向量。IPs和vector在控制台上正确输出时没有问题 为了确保调用该方法后节点仍然存在,我将它们存储在函数外部的向量中,尽管我非常确定它不会改变任何操作 我试图在循环中声明ip字符串,并将值分配给循环中的变量,因为它可能会泄露信息

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

为了确保调用该方法后节点仍然存在,我将它们存储在函数外部的向量中,尽管我非常确定它不会改变任何操作

我试图在循环中声明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();
    }
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括“IPV4.h”
int8_t计算机数量=25;
std::向量IPVec;
std::向量IPVector;
std::string FilePath=“C:/Users/Bob/Documents/visualstudio 2017/Projects/UtryonWithIrrLicht/Files/IPList.xml”;
int main(int argc,字符**argv)
{对于(大小A=0;Aappend_属性(文档分配_属性(“版本”、“1.0”);
Decl->append_属性(文档分配_属性(“编码”、“utf-8”);
单据追加节点(Decl);
rapidxml::xml_节点*列表=文档分配_节点(rapidxml::节点_元素,“列表”);
单据追加节点(列表);
对于(大小A=0;A{std::cout我自己找到了答案。
事实证明,节点构造函数不接受std::string,尽管您可以这样做,而且它在任何地方都不会产生错误。这只会产生一个难看的输出。当您将std::string作为参数传递给构造函数时,您必须将其包装在函数中。rapidxml似乎以某种方式处理该字符串,并将其修改为所需ed格式。我做了一个测试,输出非常完美

我只是换了一句话:

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()));
对于该行:

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*
,这就是没有错误的原因。