-1

I have tried to store multiple arguments through fscanf, although it works for

while(fscanf(fptr, "%s %*s %*s %*s", mod) == 1){
}

It does not work for the following

while(fscanf(fptr, "%s %s %*s %*s", mod, mod2) == 1){
}

Is there anything im doing wrong?

  • 3
    @user12908899 the return value from the `scanf` function family is the number of items successfully scanned and stored (and perhaps `0` or `EOF`). – Weather Vane Feb 16 '20 at 20:35

1 Answers1

0

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) {
        ...
    }

chqrlie
  • 131,814
  • 10
  • 121
  • 189