double number was cut off
Jul 21, 2010 at 3:52pm UTC
Dear all,
My script aims to save data in vector.
However, the original data was cut off
after the script's running.
Like this: original data is : 9.9968853238E-04,
after running, the saved data is : 0.00099688.
I tried to change from double to long double,
but same result also.
Can anybody give a suggestion ?
Thanks a lot in advance!
Junhui
The code is here:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
#include <string>
#include <cstring>
using namespace std;
int main ()
{
ifstream inFile ("../test_7_10lines.tsv" );
//ifstream inFile ("../two_ch.tsv");
if (!inFile.good() || !inFile.is_open() )
{
std::cout << "\nWARNING! Cannot read from file: " <<" ../two_ch.tsv" << std::endl;
}
string line;
int linenum = 0;
vector <double > time;
vector <double > vol;
double time_base[4096];
while (getline (inFile, line))
{
linenum++;
int itemnum = 0, j = 0;
istringstream linestream(line);
if (linenum >= 22)
{
string item;
itemnum = 0;
while (getline (linestream, item, ' ' ))
{
double time_, vol_, time_origin;
j = (linenum - 22)* 8192 + itemnum ;
itemnum ++;
if ( (itemnum % 2) != 0)
{
istringstream(item) >> time_ ;
cout << "time_ = " <<time_<<endl;
time.push_back(time_) ;
// cout << "time[j] = " <<time[j]<<endl;
}
else
{
istringstream(item) >> vol_ ;
vol.push_back(vol_);
}
}
}
}
inFile.close();
}
Jul 21, 2010 at 5:47pm UTC
You mean that it displayed on the screen using cout is not what you expect??
If so, you need to look up output stream manipulators ( fixed, scientific and setprecision() )
Jul 21, 2010 at 6:44pm UTC
Hi, guestgulkan,
Thanks a lot for your comment.
Based on your explanation, the
problem was closed.
Thanks again!
Junhui
Topic archived. No new replies allowed.