I have a huge file to parse. Previously, it was separated by either space
or comma
and I used sscanf(string, "%lf %lf ", &aa, &bb);
to get the data into my program.
But now the data format is changed to "122635.670399999","209705.752799999"
, with both comma and quotation marks. And I have no idea how to deal with it.
Actually, my previous code was found online and I had a really hard time finding proper documentation for this kind of problem. It will be great if you can recommend some to me.
Rather than read a string, then remove the commas and quotes from the strings, and finally convert the data to numbers, I'd probably create a locale object that classifies commas and quotes as white space, imbue the stream with that locale, and read the numbers without further adieu.
Using that, we can read the data something like this:
Note that once we've imbued the stream with a locale using our ctype facet, we can just read numbers as if the commas and quotes didn't exist at all. Since the ctype facet classifies them as white-space, they're completely ignored beyond acting as separators between other stuff.
I'm pointing this out primarily to make clear that there's no magic in any of the processing after that. There's nothing special about using
istream_iterator
instead of (for example)double value; infile >> value;
if you prefer to do that. You can read the numbers any of the ways you'd normally read numbers that were separated by white space -- because as far as the stream cares, that's exactly what you have.