I have data in the following format:
4:How do you do? 10:Happy birthday 1:Purple monkey dishwasher 200:The Ancestral Territorial Imperatives of the Trumpeter Swan
The number can be anywhere from 1 to 999, and the string is at most 255 characters long. I'm new to C++ and it seems a few sources recommend extracting formatted data with a stream's >>
operator, but when I want to extract a string it stops at the first whitespace character. Is there a way to configure a stream to stop parsing a string only at a newline or end-of-file? I saw that there was a getline
method to extract an entire line, but then I still have to split it up manually [with find_first_of
], don't I?
Is there an easy way to parse data in this format using only STL?
You can read the number before you use
std::getline
, which reads from a stream and stores into astd::string
object. Something like this:Its C and will work in C++ too. scanf provides more control, but no error management. So use with caution :).
Just read the data line by line (whole line) using getline and parse it.
To parse use find_first_of()
You've already been told about
std::getline
, but they didn't mention one detail that you'll probably find useful: when you callgetline
, you can also pass a parameter telling it what character to treat as the end of input. To read your number, you can use:This will put the data up to the ':' into
number
, discard the ':', and read the rest of the line intoname
.If you want to use
>>
to read the data, you can do that too, but it's a bit more difficult, and delves into an area of the standard library that most people never touch. A stream has an associatedlocale
that's used for things like formatting numbers and (importantly) determining what constitutes "white space". You can define your own locale to define the ":" as white space, and the space (" ") as not white space. Tell the stream to use that locale, and it'll let you read your data directly.Now to use it, we "imbue" the stream with a locale:
Now you know why that part of the library is so neglected. In theory, getting the standard library to do your work for you is great -- but in fact, most of the time it's easier to do this kind of job on your own instead.
The C++ String Toolkit Library (StrTk) has the following solution to your problem:
More examples can be found Here