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.

At first glance is a float enough precision for the accuracy you need?
Do you need double unless you are seing a separate number 15
I suspect that the file is being chopped as you want but you need a double.

If these are being stored from floats to be reloaded you can use a binary file instead of ascii.

I I am using the following code to read

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.

I still do a lot of stuff as in C instead of C++, and things like you are doing is one of them. So here's my C solution. You won't hurt my feelings if you don't want it.

#include <stdlib.h>
#include <stdio.h>

/*          Data Looks Like This...
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 saved this into file Data.txt
*/

struct Data
{
 double        dblFirst;
 double        dblSecond;
 double        dblThird;
 double        dblFourth;
 double        dblFifth;
 unsigned int  iSixth;
 float         fltSeventh;
};

int main()
{
 char szBuffer[256];
 unsigned int iCt=0;
 Data* pData=NULL;
 FILE* fpIn=NULL;

 fpIn=fopen("Data.txt","r");  //Open "Data.dat for read "r" access.
 if(fpIn)                     //and loop through data to count lines.
 {                            //in iCt
    while(!feof(fpIn))
    {
       fgets(szBuffer,256,fpIn);
       iCt++;
    }
    fclose(fpIn);
 }
 printf("iCt = %d\n\n",iCt);             //Allocate a buffer of Data type
 pData=(Data*)malloc(iCt*sizeof(Data));  //to hold iCt objects
 if(pData)
 {
    fpIn=fopen("Data.txt","r");  //Open "Data.dat for read "r" access.
    if(fpIn)
    {
       iCt=0;
       while(!feof(fpIn))      //read data from text file into buffer
       {
          fscanf(fpIn,"%lf%lf%lf%lf%lf%u%f",
          &pData[iCt].dblFirst,
          &pData[iCt].dblSecond,
          &pData[iCt].dblThird,
          &pData[iCt].dblFourth,
          &pData[iCt].dblFifth,
          &pData[iCt].iSixth,
          &pData[iCt].fltSeventh);
          printf("%10.2f\t%4.2f\t%f\t%f\t%f\t%u\t%4.2f\n",
          pData[iCt].dblFirst,
          pData[iCt].dblSecond,
          pData[iCt].dblThird,
          pData[iCt].dblFourth,
          pData[iCt].dblFifth,
          pData[iCt].iSixth,
          pData[iCt].fltSeventh);
          iCt++;
       }
       fclose(fpIn);
    }
    free(pData);
 }
 getchar();

 return 0;
}

/*     Output
============================================
iCt = 4

19700326.15     71.37   71.500000       70.000000       70.500000       217600  0.89
19700330.15     70.50   71.000000       70.000000       71.000000       182400  0.90
19700331.15     70.87   70.870000       70.620000       70.620000       177600  0.90
19700401.15     71.12   71.620000       71.120000       71.120000       169600  0.90
*/
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.