C++ 用测力计劈开细绳

C++ 用测力计劈开细绳,c++,arrays,string,parsing,C++,Arrays,String,Parsing,我最近一直在尝试使用TinyXML2读/写XML文件,但遇到了一个问题。我试图读取从另一个程序导出的整数数组,它会加载,但TinyXML不会读取整数数组,并且我无法将常量字符指针转换为整数 我想分离逗号分隔的值并将它们存储在数组中 我的代码如下 int GetMapData (const char* XMLFile) { int mapdata[1]; XMLDocument File; File.LoadFile(XMLFile);

我最近一直在尝试使用TinyXML2读/写XML文件,但遇到了一个问题。我试图读取从另一个程序导出的整数数组,它会加载,但TinyXML不会读取整数数组,并且我无法将常量字符指针转换为整数

我想分离逗号分隔的值并将它们存储在数组中

我的代码如下

    int GetMapData (const char* XMLFile) {
        int mapdata[1];
        XMLDocument File;
        File.LoadFile(XMLFile);
        const char* data = File.FirstChildElement("map")->FirstChildElement("layer")->FirstChildElement("data")->GetText();
}

用逗号更新

#include <sstream>

// ... 

char const *ss = "1, 2, 3, 4";  // this come from the FirstChildElement method in your case.
istringstream buffer(ss);
int value1, value2, value3, value4;
char c;
buffer >> value1 >> c >> value2 >> c >> value3 >> c >> value4;
cout << value1 << "-" << value2 << "-" << value3 << "-" << value4  << endl;

output: 
1-2-3-4
#包括
// ... 
字符常量*ss=“1,2,3,4”//这来自您案例中的FirstChildElement方法。
istringstream缓冲区(ss);
int值1、值2、值3、值4;
字符c;
缓冲区>>值1>>值c>>值2>>值c>>值3>>值c>>值4;

无法使用atoi()将字符串转换为整数。您需要分析字符串内容。你如何做到这一点取决于它的格式。@Michael从不使用atoi()做任何事情。atoi()突然出了什么问题?当然,除了旧的垃圾输入垃圾输出模式…@MichaëlRoy,你无法区分零和失败
atoi
在零或未定义输出中是垃圾。是的,类似,但值用逗号分隔,我希望它存储到数组中。谢谢我也会尝试修改代码。