469,622 Members | 1,445 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,622 developers. It's quick & easy.

InputStream Read - how to ignore comma

I have a very trivial question. But I searched in google / archives of
this group to get the answer, checked the C++ FAQ - but did not
precisely what I was looking for.

The problem is with respect to reading 'long double's from a stream
separated by
comma.
#include <iostream>
#include <sstream>
#include <string>
#include <ios>

using std::ios;
using std::string;
using std::istringstream;
using std::cout;
using std::endl;

void ProcessLine(const std::string & line) ;

int main() {
string line1 = "1,429946.2844";
string line2 = "1 429946.2844";

ProcessLine(line1);
ProcessLine(line2);

}

void ProcessLine(const std::string & line) {
istringstream iss(line);
int a;
long double b;
iss >> a >> b;
std::cout.setf(ios::fixed);
cout << line << " <-- parsed to --> " << a << " : " << b << endl;

}
The output that I am getting is:

1,429946.2844 <-- parsed to --> 1 : 0.000000
1 429946.2844 <-- parsed to --> 1 : 429946.284400

When I separate the numbers by a space everything works fine.
When I separate them by a comma things get weird.

In the original problem I read from a file so I have no control over
the input. How would I tell an inputstream to read , ignoring the comma
in the second case.

Thanks for the help.

Jul 22 '05 #1
3 4066
Rakesh Sinha wrote:
I have a very trivial question. But I searched in google / archives of this group to get the answer, checked the C++ FAQ - but did not
precisely what I was looking for.


I have posted a solution to the problem to comp.lang.c++ and
comp.lang.c++.moderated in the past several times. Using the search
string "reading comma separated iostream" on Google Groups reveals a
whole discussion of this issue, including an article I wrote presenting
an elegant solution using 'std::ctype<char>'. The URL is
<http://groups.google.de/groups?selm=5b15f8fd.0306050403.330f7743%40posting .google.com&output=gplain>.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

Jul 22 '05 #2
Thanks for the pointers Dietmar.

My code works perfectly fine, thanks to your hack. Here is how it looks
like.

#include <iostream>
#include <locale>
#include <algorithm>
#include <sstream>
#include <string>
#include <ios>

using std::ios;
using std::string;
using std::istringstream;
using std::cout;
using std::endl;

struct commactype: std::ctype<char> {

commactype(): std::ctype<char>(get_table()) {}

static std::ctype_base::mask const* get_table() {
static std::ctype_base::mask* rc = 0;

if (rc == 0) {
rc = new
std::ctype_base::mask[std::ctype<char>::table_size];
std::fill_n(rc, std::ctype<char>::table_size,
std::ctype_base::mask());
rc[','] = std::ctype_base::space;
}
return rc;
}
};
void ProcessLine(const std::string & line) ;

int main() {
string line1 = "1,429946.2844";
string line2 = "1 429946.2844";

ProcessLine(line1);
ProcessLine(line2);

}

void ProcessLine(const std::string & line) {
istringstream iss(line);
iss.imbue(std::locale(std::locale(), new commactype));
int a;
long double b;
iss >> a >> b;
std::cout.setf(ios::fixed);
cout << line << " <-- parsed to --> " << a << " : " << b << endl;

}

I have a question though. Once we change the locale using 'imbue' ,
would it be still valid / possible to return to the default locale ?
Thanks for your help.

Jul 22 '05 #3
Rakesh Sinha wrote:
I have a question though. Once we change the locale using 'imbue' ,
would it be still valid / possible to return to the default locale ?


Yes. You can simply 'imbue()' another locale. There is only a minor
restriction that you cannot change the code conversion facet of a
file stream once you have opened it (actually, the restriction is
that you cannot change the code conversion facet once you have read
or written to an opened stream but a popular implementation makes the
stricter assumption) but there is rarely a need to change the code
conversion facet at all and different locales can share common facets.

The only possible problem could be obtaining the original locale: you
can either obtain it prior to a call to 'imbue()' using 'getloc()' or
you can rely on the default, i.e. the global locale, not being
changed. The global locale can simply be obtained with locale's default
constructor: 'std::locale()'.

Note that switching locales can be a relatively costly operation since
the stream might cache values obtained from locale facets: assuming
that locales rarelsy change, it is reasonable to prepare various data
just once rather than obtaining it for many IOStream operations.
Anyway,
if you need different locales with the same stream, you are probably
better off creating multiple streams sharing a common stream buffer.
For example:

| // ...
| std::istringstream input1("some string");
| std::istream input2(input1.rdbuf());
| input2.imbue(some_locale);
| // ...

The stream objects themselves don't buffer any characters: this is
done only by the stream buffers. Thus, you can alternatingly obtain
characters from either 'std::istream' avoiding the cost of switching
locales back and forth.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by sea | last post: by
1 post views Thread by Charles.Deisler | last post: by
reply views Thread by bvasanth123 | last post: by
reply views Thread by bvasanth123 | last post: by
4 posts views Thread by macap.usenet | last post: by
11 posts views Thread by John Krukoff | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.