I know how to do it with get/getline and the like. Changing the delimiter
in the input string just seems more elegant.
Thanks!
Or more confusing to the reader.
You can modify the locale to consider commas (and only commas)
whitespeace. This still won't allow you to read CSV, since it
will treat commas in a quoted string as separators, and will
treat empty fields (two successive commas) as non-existant.
It's also a hack, and abuse of the locale. As such, it can only
server to confuse the reader.
--
James Kanze (GABI Software) email:james...@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
> Or more confusing to the reader.
>
> You can modify the locale to consider commas (and only commas)
> whitespeace. This still won't allow you to read CSV, since it will
> treat commas in a quoted string as separators, and will treat empty
> fields (two successive commas) as non-existant.
>
> It's also a hack, and abuse of the locale. As such, it can only server
> to confuse the reader.
So how would I do it anyway? I am aware of commas within quotes, but 100%
my files do not have any quotes, nor there are empty fields. Oh, and the
reader is myself. It wouldn't confuse me. No more than usual.
> It's also a hack, and abuse of the locale. As such, it can only server
> to confuse the reader.
Meanwhile I've turned to boost tokenizer to parse a line, which can handle
commas within quotes too, so doing it using facets is only a matter of
curiosity now.
In other words, you're using a tool designed for the job, rather
than a tool designed for a different job.
FWIW, if you do want to change the definition of what istream
thinks is white space, you have to:
1. Create an instance of a ctype<char> facet which defines
white space as you want. Exceptionally (because creating
new facets is normally anything but trivial), this is
simple; the standard provides a specialization of ctype for
char, which has a constructor which takes a table specifying
the types of each char, so all you have to do is write the
table and create an instance of this specialization.
Note that locale reference counts facets, so you should use
new to create the instance, and let locale itself take care
of the delete.
2. Create a locale using this facet. For this, you'd probably
use the templated constructor template< typename Facet >
std::locale::locale( std::locale const& other, Facet* ),
which creates a copy of other, with the given facet
replacing the one in the original locale.
3. Imbue your stream with this locale.
There's an example here:
http://groups.google.com/group/alt.comp.lang.learn.c-
c++/msg/db55ffea728c5cf9?dmode=source
Note that this does have a few minor shortcomings with respect to normal
CSV files. For one example, a comma acts JUST like whitespace in a
stream, which means something like "9,,3" is read as simply a 9 followed
by a 3, where this would typically be interpreted as an empty field
between the 9 and the 3.
--
Later,
Jerry.
The universe is a figment of its own imagination.
> There's an example here:
>
> http://groups.google.com/group/alt.comp.lang.learn.c-
> c++/msg/db55ffea728c5cf9?dmode=source
>
Thank you! It's exactly what I was looking for - how to create a facet
with , instead of space.