-1

This is a snippet from the file it reads of:

CORINE WILBURN 05 10 12 14 25
PETER BOLDEN 02 10 22 23 27
RAPHAEL ESPINOSA 05 09 11 25 30
ALEXA VARNEY 02 07 09 12 24
STANLEY GRUBBS 03 04 12 26 30
ARMIDA RODGERS 12 16 17 20 29
HOPE LOVE 04 06 12 19 25

This is the code that reads it:

while(!feof(f)){
        fscanf(f, "%s %s %d %d %d %d %d \n", first, last, &numbers[0], &numbers[1], &numbers[2], &numbers[3], &numbers[4]);
        printf("%s %s %d %d %d %d %d\n", first, last, &numbers[0], &numbers[1], &numbers[2], &numbers[3], &numbers[4]);
}

This is what printf gives over and over:

CORINE WILBURN 6421984 6421988 6421992 6421996 6422000
CORINE WILBURN 6421984 6421988 6421992 6421996 6422000
CORINE WILBURN 6421984 6421988 6421992 6421996 6422000
CORINE WILBURN 6421984 6421988 6421992 6421996 6422000
CORINE WILBURN 6421984 6421988 6421992 6421996 6422000
Galorch
  • 21
  • 2
  • 3
    First problem: Remove the trailing whitespace and `\n` from the `fscanf` format string. That doesn't do what you think it does: [What is the effect of trailing white space in a scanf() format string?](https://stackoverflow.com/questions/19499060/what-is-the-effect-of-trailing-white-space-in-a-scanf-format-string) – kaylum Oct 09 '22 at 19:59
  • 3
    Second problem: Remove all the `&`'s from the `printf`. – kaylum Oct 09 '22 at 20:00
  • 3
    Third problem: [Why is “while( !feof(file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feoffile-always-wrong) – kaylum Oct 09 '22 at 20:00
  • Fourth problem: always check `fscanf`'s return value to learn whether it successfully read what you you wanted it to. – Steve Summit Oct 10 '22 at 04:03

1 Answers1

0

I suspect all but the first fscanf() is failing (code below checks the return value), or you read into new set of variables but print the first one out in each iteration. You also pass the address of the variables instead of the value hence the numbers are probably not what you expect. Updated format string to avoid buffer overflow.

#include <stdio.h>
#define LEN 40
#define str(s) str2(s)
#define str2(s) #s

int main(void) {
    FILE *f = fopen("input.txt", "r");
    if(!f) return 1;
    for(;;) {
        char first[LEN+1];
        char last[LEN+1];
        int numbers[5];
        int r = fscanf(f, "%" str(LEN) "s%" str(LEN) "s%d%d%d%d%d",
            first, last,
            &numbers[0], &numbers[1], &numbers[2],
            &numbers[3], &numbers[4]);
        if(r == EOF) break;
        if(r != 7) {
            printf("scanf failed\n");
            fclose(f);
            return 1;
        }
        printf("%s %s %d %d %d %d %d\n",
            first, last,
            numbers[0], numbers[1], numbers[2],
            numbers[3], numbers[4]);
    }
    fclose(f);
}
Allan Wind
  • 23,068
  • 5
  • 28
  • 38