Advertisement

Parsing lines stored in std::string's

Started by May 02, 2001 03:28 PM
1 comment, last by TeTE 23 years, 9 months ago
I have to parse a text file with lines similar to that: f 1/3/4 2/3/8 2/4/3 I store the line in a std::string. The question is: how can I store the numbers in integer variables? How can I mimic C''s scanf function? Thanks!
Have you tried using std::istrstream?

      std:string strNumbers ("1 2");int nNumber1;int nNumber2;std::istrstream stream (strNumbers.c_str());stream >> nNumber1 >> nNumber2;  // Etc...ASSERT(nNumber1 == 1 && nNumber2 == 2);      


It's not quite sscanf(), but it works well with std::string.

Edited by - I-Shaolin on May 2, 2001 8:15:17 PM
Advertisement
You can also use the string.find () function to find the position of the next delimiter (your / ) in this case, and retrive a C style string (null terminated array of char) with string.c_str() to pass to atoi(). Then again you can just use the sting.c_str () function to get a C style string and use scanf() to parse it.

This topic is closed to new replies.

Advertisement