InputStream Read - how to ignore comma

R

Rakesh Sinha

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

Dietmar Kuehl

Rakesh said:
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/[email protected]&output=gplain>.
 
R

Rakesh Sinha

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

Dietmar Kuehl

Rakesh said:
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.
 

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,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top