使用 rapidxml::xml_node::first_attribute 时出现分段错误<char>
Segmentation fault when using rapidxml::xml_node<char>::first_attribute
我正在学习使用 rapidXML,但是当我尝试读取大型 XML 文件时(我将其发布在 pastebin 上,因为它有 400 多行长),当第 78 行循环第 4 次时,我遇到了分段错误,所以我认为它有点像小缓冲区,但我不知道该怎么办。我的代码:
#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 格式似乎不正确。当我删除开始数据标记之后和结束标记之前的新行时,程序按预期执行。没有抛出任何错误。
相关文章:
- 在某些循环内使用vector.push_back时出现分段错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- 当我的阵列太大时出现分段错误
- 分段错误当我试图运行程序时出错
- 在c++中初始化矩阵时出现分段错误(核心转储)
- 尝试使用集合函数时出现分段错误
- 我无法缩小此分段错误的原因
- g++的分段错误(在NaN上使用to_string两次时)
- 为什么我使用 std::copy() 收到运行时错误(分段错误)?
- 为什么我的代码包含错误分段错误(核心转储)?
- 为什么会出现此错误?- 分段错误(核心转储)
- 意外的运行时错误(分段错误)
- C++将文本文件读入数组,运行错误:分段错误
- mmap 错误:分段错误/指针无效错误
- 在我的代码中找不到错误....分段错误
- 错误:分段故障(核心转储)阵列
- 用string.h将错误分段到队列中
- c++字符串流转换错误:分段错误
- 为什么我的代码显示错误分段错误(核心转储)