are r0-r3 significant or just some marker? I mean, do you need to process and store them too? If not,
char buf[80];
int data[4] = { 0 };
FILE *fp = fopen("input.txt", "r");
if(fp) {
while(fgets(buf, sizeof buf, fp) != NULL) {
if((sscanf(buf, "%d %d %d %d",
&data[0], &data[1], &data[2], &data[3])) == 4) {
// data[0-4] contain current line in file
} else {
// ints weren't converted and stored
// r0-r3 will take this path
}
}
fclose(fp);
}
"data" need not be an array; it can be four separate variables. if later you want the floats, change the sscanf to %f instead of %d and change the ints to floats.
oops; didn't catch values were for later use right away. If you need to keep and store each line in the file for later use, you can do a couple of things:
calculate and store results for each line inside of the while loop... you'd need an array or if know there's only two lines of ints, just use two variables and count the lines in the file.
another thing you could do is use a 2D array for ints.
int data[NUM_OF_LINES_TO_STORE][4] = { { 0 } },
idx = 0;
while(fgets(...)) {
if((sscanf(buf, "%d %d %d %d",
&data[idx][0], &data[idx][1], etc)) == 4) {
// increment idx on successful storing
}
}
something like that.
HTH