changing istream default delimiter

A

Amadeus W.M.

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!
 
J

James Kanze

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.

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

Amadeus W.M.

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

Amadeus W.M.

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

Jerry Coffin

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.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top