I am trying to read from a file with this text inside:
f 502 601 596 465 464
f 597 599 600 598
f 602 591 596 601
f 588 565 548 260 62 61 595
f 583 595 61 558 561
f 237 241 471
On each line, there is an f followed by a random amount of floats. I want to be able to take the numbers after the f and store them in an array of structs of floats. I wrote code that will parse the text file if there is three floats on each line but now I am instructed to do it if there is a random amount of floats (up to 13 floats). Basically the code I have now for three floats on every line is as follows:
struct faces {
float p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12;
}
struct faces eFace[100];
FILE *fp;
char buff[128];
int fCount = 0;
fp = fopen("text.txt", "r");
if (fp == NULL)
printf("Can't open file");
else {
while (fgets(buff, sizeof(buff), fp) != NULL) {
if (buff[0] == 'f') {
sscanf(buff, "f %f %f %f", &eFace[fCount].p1, &eFace[fCount].p2, &eFace[fCount].p3);
fCount++;
}
}
}
fclose(fp);
}
What would be the best way to modify my code so that it takes every float (up to 13 floats) after "f" until a new line and stores them in the array of the structs I made? I appreciate any help and if you need more information just let me know!
Note: I always have to check if the line starts with an f.