简体   繁体   中英

C++ stringstream conversion to double with operator>>

I'm trying to read a column file in C++ by overloading the stream input operator. This is the main function to read each line in the file:

#include <fstream>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
#include <vector>

using namespace std;
typedef vector <double> record_t;

istream& operator >> ( istream& ins, record_t& record )
{
  record.clear();

  string line;
  getline( ins, line );

  stringstream ss( line );
  double f;
  while (ss >> f)
    record.push_back(f);

  return ins;
}

which works fairly well in most cases.
But for this line

55000.003520    -0.009740   0.000721    -0.024600   0.000000

the first field is stored as 55000.0 instead of 55000.003520
Is there any way I can make it read the correct value with all the significant digits?

Thanks in advance for your help!

To archive the answer to the problem:

The number is actually read in and stored correctly. The way OP checked was by simply piping it to cout , which has a very limited default output precision, so many of the digits were just "rounded away" for printing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM