Reputation: 13
I want to read each of the 4 columns stored in a .txt file separated by a space into an array of their own. The text file may have hundreds of rows so reading until the end of file is desirable.
Example:
3.4407280e+003 6.0117545e+003 8.0132664e+002 2.5292922e+003
3.4163843e+003 5.9879421e+003 7.7792044e+002 2.5058547e+003
so typically it would be array 1 containing all the rows from the first and most left column and so on.
Upvotes: 1
Views: 4588
Reputation: 55533
fscanf
is your friend:
static const int MAX_FILE_ROWS = 200;
double lines[MAX_FILE_ROWS][4];
FILE *file = fopen("myfile.txt", "r");
for (int i = 0; i < MAX_FILE_ROWS; i++)
{
if (feof(file))
break;
fscanf(file, "%lf %lf %lf %lf", &(lines[i][0]), &(lines[i][1]), &(lines[i][2]), &(lines[i][3]));
}
fclose(file);
Then, lines should contain the data you need.
Upvotes: 2