read from file into array

Hi ,
I have this code which is capable to read from file only one column , I need it read two column. So, how can I alter it to read from file and place the value in array ?
Note : the data file format like that :

10.2 30.3 12.4 ........
34.56 45.9 34.5.......
12.3 12.45 10.10.....
.
.
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
QTextStream in(&file);
     unsigned int max_lines = 20000; // we choose 102 because in the real file there are two header lines in the top of file
     float data[max_lines];
     unsigned int lines_read =0;

     for(int i=lines_read ;i<max_lines;i++)
     {
        double x;
        QString content;
        in >> x;
        content = content.setNum(x);

        data[i]=content.toDouble();
        in.readLine();
        lines_read++;
     }
The body of the for loop is confusing me, why do you need to go through all of that rather than simply casting to a float? Is there a hardware limitation that makes that slow? I can't see a cast to a string and back again being faster than the built int float<>double casting even if it's done in software.

Anyways, back to the question. The simplest would simply be to make data a multi-dimensional array:

1
2
3
4
5
6
7
8
float data[max_lines][2];
...
for ( each row ) {
//read first col
data[i][0] = //value from first col
//read second col
data[i][1] = //value from second col
}
Last edited on
Hi stravant ,

many thanks for ur post. it is realy easy and helpful
Topic archived. No new replies allowed.