Using stringstream instead of `sscanf` to parse a fixed-format string
- by John Dibling
I would like to use the facilities provided by stringstream to extract values from a fixed-format string as a type-safe alternative to sscanf. How can I do this?
Consider the following specific use case. I have a std::string in the following fixed format:
YYYYMMDDHHMMSSmmm
Where:
YYYY = 4 digits representing the year
MM = 2 digits representing the month ('0' padded to 2 characters)
DD = 2 digits representing the day ('0' padded to 2 characters)
HH = 2 digits representing the hour ('0' padded to 2 characters)
MM = 2 digits representing the minute ('0' padded to 2 characters)
SS = 2 digits representing the second ('0' padded to 2 characters)
mmm = 3 digits representing the milliseconds ('0' padded to 3 characters)
Previously I was doing something along these lines:
string s = "20101220110651184";
unsigned year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0, milli = 0;
sscanf(s.c_str(), "%4u%2u%2u%2u%2u%2u%3u", &year, &month, &day, &hour, &minute, &second, &milli );
The width values are magic numbers, and that's ok. I'd like to use streams to extract these values and convert them to unsigneds in the interest of type safety. But when I try this:
stringstream ss;
ss << "20101220110651184";
ss >> setw(4) >> year;
year retains the value 0. It should be 2010.
How do I do what I'm trying to do? I can't use Boost or any other 3rd party library, nor can I use C++0x.