[QUOTE=Pinkysuavo;1254527And that's where my problem comes from. It reads the number from next line and I want to avoid that.[/QUOTE]
That's why I suggested using fgets to read the entire line first, then use sscanf on the line.
Untested code:
Code:
char line[1000];
while (fgets(line, sizeof line, fp) != NULL) {
int ret = sscanf(line, "%*d %c %d %d %d %d", &c, &w, &x, &y, &z);
if (ret == 3) {
// letter and two ints
} else if (ret == 5) {
// letter and four ints
} else {
// error
}
}
EDIT: A little test program
Code:
/*
1 A 1 2
2 A 3 4
3 A 7 8 9 10
x x x x
*/
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *fp = fopen("cb_reading_from_file.c", "r"); // this source file
fgetc(fp); fgetc(fp); fgetc(fp); // skip first line
char line[1000], c;
int w, x, y, z;
while (fgets(line, sizeof line, fp) != NULL) {
int ret = sscanf(line, "%*d %c %d %d %d %d", &c, &w, &x, &y, &z);
if (ret == 3) {
printf("2 vals: %c %d %d\n", c, w, x);
} else if (ret == 5) {
printf("4 vals: %c %d %d %d %d\n", c, w, x, y, z);
} else {
fprintf(stderr, "Bad line: %s", line);
exit(EXIT_FAILURE);
}
}
fclose(fp);
return 0;
}
Output:
Code:
2 vals: A 1 2
2 vals: A 3 4
4 vals: A 7 8 9 10
Bad line: x x x x