I am having a most mysterious problem which I have been unable to solve after repeated attempts. Perhaps somebody here will be smart enough to figure out what is going on.
I am attempting to read a file of this format
19700326.15 71.37 71.50 70.00 70.50 217600 0.89
19700330.15 70.50 71.00 70.00 71.00 182400 0.90
19700331.15 70.87 70.87 70.62 70.62 177600 0.90
19700401.15 71.12 71.62 71.12 71.12 169600 0.90
I am using the following code to read this array of numbers into a 2D float array.
vector < vector < float > > info;
ifstream file(filename.c_str());
string line;
int length;
while ( getline(file, line) )
{
vector < float > data;
float value;
istringstream iss(line,istringstream::in);
while (iss >> value)
{
length=0;
data.push_back(value);
if(data.size()>=length)
{
length=data.size();
}
}
info.push_back(data);
}
When I go to check the data within the arrays, I see something very very strange.
When I use
std::cout<<std::setprecision(11)<<info[0][0]<<endl;
etc,
I get 1970326 instead of 19700326.15
Somehow, the decimals at the end are being truncated!!
This only happens to the first column of data, the other columns are read okay!
Further, sometimes, a number like 19840907 in input file will changed to 19840908 in the vector and this seems to happen randomly.
Can anybody tell me what the heck is going on? This is driving me insane.