473,386 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 4297
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: sea | last post by:
I have text files in the following format: 123,34, ,345,890, 123,23 .. .. .. As you can see, the problem is that (1) the commas can occur in
1
by: Charles.Deisler | last post by:
XmlTextReader requestdata = new XmlTextReader(Request.InputStream); XmlTextWriter xmltextwriter = new XmlTextWriter(somefile,someencoding); XmlTextWriter xmltextwriter2 = new...
0
by: bvasanth123 | last post by:
Hi, I would like to know how can I read an InputStream into Excel object. In my web app, user uploads MS-Excel file though asp.net html file control. I get UploadedFile.PostedFile.InputStream()...
0
by: bvasanth123 | last post by:
Hi, I would like to know how can I read an InputStream into Excel object. In my web app, user uploads MS-Excel file though asp.net html file control. I get UploadedFile.PostedFile.InputStream()...
16
by: lovecreatesbeauty | last post by:
/* When should we worry about the unwanted chars in input stream? Can we predicate this kind of behavior and prevent it before debugging and testing? What's the guideline for dealing with it? ...
1
by: O.B. | last post by:
I have the need to read a file (version.txt) that is embedded within an WAR file that is within an EAR file: foo.ear contains bar.war contains resources/version.txt I am able to get the bar.ear...
0
by: raheel javed | last post by:
i am using mobile for communication with live server when mobile send its information to server the input stream is encoded in utf-8 that is converted into string but it is still not...
4
by: macap.usenet | last post by:
Hello, I´ve a strange problem with my HttpPostedFile object. I coded a File Upload where the user can upload a zip file. My code looks like this: HttpFileCollection files =...
11
by: John Krukoff | last post by:
-----Original Message----- First off, ignore castironpi, it's a turing test failure. Second, I'm curious as to how Java manages this. I'd think their streams would have to be pretty magic to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.