Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

changing istream default delimiter

205 views
Skip to first unread message

Amadeus W.M.

unread,
Mar 29, 2009, 10:50:08 PM3/29/09
to
Is it possible to change the space delimiter in an istream to, say, a
comma? I'm thinking facets, locales and imbue. I want to read a comma
separated file in the exact same way as if it were tab delimited.
If yes, how would I do that?

I know how to do it with get/getline and the like. Changing the delimiter
in the input string just seems more elegant.

Thanks!

James Kanze

unread,
Mar 30, 2009, 5:39:21 AM3/30/09
to

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

Amadeus W.M.

unread,
Mar 30, 2009, 11:15:06 PM3/30/09
to
On Mon, 30 Mar 2009 02:39:21 -0700, James Kanze wrote:

> 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.

Amadeus W.M.

unread,
Mar 30, 2009, 11:56:11 PM3/30/09
to
On Mon, 30 Mar 2009 02:39:21 -0700, James Kanze wrote:

> 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.

James Kanze

unread,
Mar 31, 2009, 6:29:01 AM3/31/09
to

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.

Jerry Coffin

unread,
Apr 2, 2009, 4:43:46 PM4/2/09
to
In article <pan.2009.03...@verizon.net>, amad...@verizon.net
says...

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.

Amadeus W.M.

unread,
Apr 3, 2009, 12:10:02 AM4/3/09
to
On Thu, 02 Apr 2009 14:43:46 -0600, Jerry Coffin wrote:

> 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.

0 new messages