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