Use vector<std::string>
instead of
vector<char*>
then, use string.c_str()
to convert string
to
char*
Question
I cannot return the loadings from the xml. Here is the code:
RapidXML xml loader
vector<char*> ParseFanbeamGeometry(char* fileName)
{
xml_document<> doc;
xml_node<> * root_node;
// Read the xml file into a vector
ifstream theFile (fileName);
vector<char> buffer((istreambuf_iterator<char>(theFile)), istreambuf_iterator<char>());
buffer.push_back('\0');
// Parse the buffer using the xml file parsing library into doc
doc.parse<0>(&buffer[0]);
// Find our root node
root_node = doc.first_node("FanbeamGeometrySettings");
vector<char*> fambeamGeometry;
// Iterate over the fangeometry
for (xml_node<> * fanGeometry_node = root_node->first_node("FanGeometry"); fanGeometry_node; fanGeometry_node = fanGeometry_node->next_sibling())
{
try
{
fambeamGeometry.push_back(fanGeometry_node->first_attribute("geometry")->value());
fambeamGeometry.push_back(fanGeometry_node->first_attribute("D")->value());
}
catch (exception &e)
{
e.what();
}
}
return fambeamGeometry;
}
main function
int _tmain(int argc, _TCHAR* argv[])
{
char* fangeofile = "D:\\Projects\\fanbeam\\tools\\rapidxml-1.13\\GeometrySettings.xml";
vector<char*> fambeamGeometry = ParseFanbeamGeometry(fangeofile);
float D = atof(fambeamGeometry[1])*N;
getchar();
return 0;
}
xml file
<?xml version="1.0"?>
<FanbeamGeometrySettings>
<FanGeometry geometry="EquiDistance" D="2"/>
</FanbeamGeometrySettings>
The fambeamGeometry properly loads "EquiDistance" and "2", but when it is returned from ParseFanbeamGeometry() to main, the value becomes some weird values, like -18 and -2.
Any suggestions? I have no idea why. How to let the xml loader return what it loads? Thanks.
Solution 2
Use vector<std::string>
instead of
vector<char*>
then, use string.c_str()
to convert string
to
char*
OTHER TIPS
Your function ParseFanbeamGeometry
is returning a vector of pointers, but the data that they point to will be invalid once buffer
gets destructed as the function returns.
The correct approach, as you've realized is to pass back std::string
s instead. That's the idiomatic approach in C++ anyway.
vector<std::string> ParseFanbeamGeometry(const std::string &fileName)