fscanf()
returns the number of conversions that were successfully stored.
Hence you should compare the result of the second fscanf()
to 2
:
while (fscanf(fptr, "%s %s %*s %*s", mod, mod2) == 2) {
...
}
Note however that the spaces are redundant and the words skipped by the %*s
conversion specifiers may just be absent at the end of file without causing a different return value.
Note too that you should tell fscanf
the maximum number of bytes to store into the destination arrays mod
and mod2
to avoid undefined behavior on unexpectedly large words in the input file:
char mod[32], mod2[32];
while (fscanf(fptr, "%31s %31s %*s %*s", mod, mod2) == 2) {
...
}