I am reading a text file of two-column numeric data. I do not know the number of rows that the file will contain. Currently, I read the file twice: Once to ascertain the length and the second time to fill the arrays with the data. Is there a way to create and populate the arrays while only having to read the text file once?
Here is my current code:
Code:
FILE* fid;
double tempx, tempy;
fid = fopen("c:/file.txt","rt");
int i=0;
while( fscanf(fid,"%le%le", &tempx, &tempy )!= EOF )
i++;
rewind( fid );
x = new double[i];
y = new double[i];
i=0;
while (fscanf(fid,"%le%le", x+i, y+i)!= EOF)
i++;
fclose(fid);
Thanks for your time.