I am trying in vain to find a way to parse a text file stored in a string object. The format of the string is as follows:
...
1 45
1 46
1 47
2 43
2 44
2 45
...
I am trying to iterate over the whole string, grab each line, and then split the string by the first integer and the second integer for further processing. However, doing something like this doesn’t work:
string fileContents;
string::iterator index;
for(index = fileContents.begin(); index != fileContents.end(); ++index)
{
cout << (*index); // this works as expected
// grab a substring representing one line of the file
string temp = (*index); // error: trying to assign const char to const char*
}
I am trying to find a way to do this, but so far I haven’t had any luck.
Use
istringstreams andstd::getline()to obtain the integers from each line:See demo at http://ideone.com/mFmynj .