-1

I've got trouble reading a text file with Qt. My text file looks like this:

17,9001500000000    31,1151700000000    450 0   NaN NaN NaN NaN NaN NaN
1   1   1390309311,00000    0,999847695156391   0,999847695156391   0,999847695156391   0,999847695156391   0,999847695156391   0,999847695156391   0,999847695156391
1   2   1390309311,00000    0,999695413509548   0,999695413509548   0,999695413509548   0,999695413509548   0,999695413509548   0,999695413509548   0,999695413509548

and so on..

Data is separated by white space

First row: The first columns of the first row are written with information I need in general for the following rows. Not every coloumn in the first row is used. After a few information the columns are filled with "NaN".

Next rows: Here my data is being placed. I want to read all of it line by line. The first 3 columns are again general information for the line, the following (lets say 300 columns) are filled with 3 different kind of data (every kind of the size of 100 measurements).

So I read my file: First, if I read in the first line I want to recieve the general information for the whole file. (info1, info2, info3, info4) Then I read the following lines, separated by white space.

if (!File.open(QIODevice::ReadOnly | QIODevice::Text))
            return false;

        QTextStream in(&metFile);
        QString line;
        int linecount = 0;

        double info1,info2,info3,info4;
double lineinfo1,lineinfo2,lineinfo3;

        while (!in.atEnd()) {

            if (linecount==0) {

                    in >> info1 >>  info2 >>  info3 >> info4;
                    linecount++;

            }
            else {

            line = in.readLine();

            QStringList lineparts = line.split(QRegExp("\\s+"));

lineinfo1=lineparts[0].toFloat;
lineinfo2=lineparts[1].toFloat;
lineinfo3=lineparts[2].toFloat;

            for (int count=1; count<=100;count++) {

                    float data1=lineparts[count+2].toFloat();
                    float data2=lineparts[100+count+2].toFloat();
                    float data3=lineparts[2*100+count+2].toFloat();

            }

            linecount++;

            }

}

Now, info1-4 are read correctly. But after that lineinfo1-3 and data1-3 (lineparts[0], etc.) are filled with nan or 0?!

Can someone help me?

Thank you!

smaica
  • 723
  • 2
  • 11
  • 26

1 Answers1

0

Here is my solution to try:

Just modify the code to and my guess is that should work:

 if (linecount==0) {

    in >> info1 >>  info2 >>  info3 >> info4;
    QString  remaining_line = in.readLine();  //This should pass from NaN's to next line inside your else
    linecount++;

 }

My guess is, in your code after reading first four numbers the control is still on the first line and inside "else" when you read line it reads remaining line where there are only NaN's.

The best way when writing file reading codes is you should always print what you are reading when testing your code. Let me know if it works for you .

Santosh

Santosh
  • 147
  • 7