繁体   English   中英

使用 Rapidxml::xml_node 时出现分段错误<char> ::first_attribute

[英]Segmentation fault when using rapidxml::xml_node<char>::first_attribute

我正在学习使用 RapidXML,但是当我尝试读取大XML 文件时(我将它发布在 pastebin 上,因为它超过 400 行)当它第 4 次循环时,我在第 78 行出现分段错误,所以我认为它是像小缓冲区之类的东西,但我不知道该怎么做。 我的代码:

#include <rapidxml/rapidxml.hpp>
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <glm/glm.hpp>
#include <algorithm>
#include <vector>

using namespace rapidxml;

int main(){
    std::ifstream fule("./xml.xml");
    if(!fule){
        std::cout << "failed to open file";
    }
    std::string token, word;//to count number of layers
    word = "</layer>";
    int noLayers = 0;
    while(fule>>token){
        if(word==token){
            noLayers++;
        }
    }
    std::cout << std::endl << noLayers << std::endl;
    fule.close();
    std::ifstream file("./xml.xml");
    if(!file){
        std::cout << "failed to open file";
    }
    std::stringstream buffer;
    buffer << file.rdbuf();
    xml_document<> doc;
    std::string content(buffer.str());
    doc.parse<0>(&content[0]);
    file.close();


    xml_node<> *pRoot = doc.first_node();//<in this case <map> node, get root node
    std::string gameVersion, orientation, renderOrder; //used later
    int mapWidth, mapHeight, tileWidth, tileHeight, playerStartPositionX, playerStartPositionY;
   
    xml_attribute<> *pAttr = pRoot->first_attribute(); 
    pAttr = pRoot->first_attribute("version");
    gameVersion = pAttr->value();
    pAttr = pRoot->first_attribute("orientation");
    orientation = pAttr->value();
    pAttr = pRoot->first_attribute("renderorder");
    renderOrder = pRoot->value();

    pAttr = pRoot->first_attribute("width");
    mapWidth = atoi(pAttr->value());
    pAttr = pRoot->first_attribute("height");
    mapHeight = atoi(pAttr->value()); 
    pAttr = pRoot->first_attribute("tilewidth");
    tileWidth = atoi(pAttr->value());
    pAttr = pRoot->first_attribute("tileheight");
    tileHeight = atoi(pAttr->value());
    xml_node<> *pNode = pRoot->first_node("properties");
    for(xml_node<> *propNode = pNode->first_node("property"); propNode; propNode = propNode->next_sibling()){
        pAttr = propNode->first_attribute("name");
        std::string s = pAttr->value();
        if(s == "PlayerStartPositionX"){
            pAttr = propNode->first_attribute("value");
            playerStartPositionX = atoi(pAttr->value());
        }if(s == "PlayerStartPositionY"){
            pAttr = propNode->first_attribute("value");
            playerStartPositionY = atoi(pAttr->value());
        }
    }
   
    int i= 0;//to count loops of next for loop 
    int m_levelData[mapWidth][mapHeight][noLayers];//make it [sizeX][sizeY][numOfLayers]
    for(pNode = pRoot->first_node("layer"); pNode; pNode = pNode->next_sibling()){
    //TODO: get layer info <++> 
    xml_node<> *dataNode = pNode->first_node("data");dataNode;
    pAttr = dataNode->first_attribute("encoding");
    std::string s = pAttr->value();
    std::vector<std::string> levelData;
    std::string tmp;
    if(s == "csv"){
        s = dataNode->value();
        std::replace(s.begin(), s.end(), ',', ' ');
        std::stringstream ss(s);
        while(std::getline(ss, tmp)){
            if(!tmp.empty()){
            levelData.push_back(tmp);
            }
        }
            for(int j = 0; j < /*sizeY*/ 100; j++){
                std::stringstream in(levelData[j]);
                std::vector<int> v;
                int temp;
                while(in >> temp){
                    v.push_back(temp);
                } 
                for(int k = 0; k < /*sizex*/ 100;k++){
                   m_levelData[k][j][i] = v[k]; 
                }
            }
    } else {
        std::cout << "*AUTISTIC SCREAMING*" << std::endl;
    }
    i++;
    }
}

(这只是用于了解rapidXML 工作原理的临时代码)

编辑:

XML 文件由平铺地图编辑器生成

看来您的 XML 格式不正确。 当我在开始数据标记之后和结束标记之前删除新行时,程序按预期执行。 没有抛出错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM