PDA

View Full Version : read from file into array



obad
1st August 2010, 07:49
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.....
.
.
.

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++;
}

SixDegrees
1st August 2010, 08:26
Read both of the values you want into variables within your loop body. If you need to skip a value in the middle, read it into a dummy variable.\\

Why are you using both the input operator (>>) and readline()? Also, why not just read directly into your array instead of performing all the conversions? And since data[] is an array of float, why are you converting to a double?