0
int lire(int indice1) {
    int indice2 = 0;
    char c[1000];
    FILE *fptr;
    FILE *indice2r;
    indice1 = 0;
    fptr = fopen("Participant.txt", "r");
    if (fscanf(fptr, "%d", &indice1) == EOF) {
        return (indice1);
    } else {
        while (fscanf(fptr, "%d", &indice1) != EOF) {
            indice2r = fopen("indice2.txt", "w+");
            fscanf(indice2r, "%d", &indice2);
            for (indice2 = 0; c[indice2] != '\n'; indice2++) {
                fscanf(fptr, "%c", &c[indice2]);
            }
            fscanf(fptr, "%d", &indice1);
            indice1++;
            fprintf(indice2r, "%d", indice2);
        }
        fclose(indice2r);
        return indice1;
    }
}

My problem is that whenever my program goes into the for function that is supposed to stop after reading '\n' it keeps going and doesn't leave the loop.

jww
  • 97,681
  • 90
  • 411
  • 885
  • `fscanf(fptr,"%c",&c[indice2]);` is the culprit, add a space before `%c`. Please see [scanf() leaves the newline char in buffer?](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-buffer) and the same applies for `fscanf`. – Weather Vane Apr 28 '18 at 19:22
  • You read `indice2` with `fscanf(indice2r,"%d",&indice2)` and then set it to zero on the next line: `for(indice2 = 0; ...` – user3386109 Apr 28 '18 at 19:23
  • `fscanf(indice2r, "%d", &indice2);` -- check the return. If you don't, you have no clue that `indice2` is valid. – David C. Rankin Apr 28 '18 at 19:25
  • The loop condition `while (fscanf(fptr, "%d", &indice1) != EOF)` is dubious — it will run continuously if there is a non-numeric graphic character such as a letter in the input because `fscanf()` will return 0 (it couldn't find a number), which is not EOF, but it still can't read anything, and the problem keeps going. You have a second (untested) `fscanf(fptr, "%d", &indice1);` further down the code — that's curious, because you end up reading (attempting to read) two numbers in a row. That probably isn't what you intended, either. – Jonathan Leffler Apr 28 '18 at 19:40
  • 1
    Possible duplicate of [scanf() leaves the new line char in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer) – MFisherKDX Apr 28 '18 at 20:12

2 Answers2

2

indice2 is incremented before it is compared so you are not comparing against read value but index after that I believe.

Xenon
  • 189
  • 1
  • 1
  • 14
1

Do not compare the return value of fscanf() to EOF, instead compare to the expected number of conversions.

Furthermore, there is a major confusion in this code:

        fscanf(indice2r, "%d", &indice2)
        for (indice2 = 0; c[indice2] != '\n'; indice2++) {
            fscanf(fptr, "%c", &c[indice2]);
        }

You read an integer into indice2, then use the same variable to scan for the end of line. You should use a different approach to skipping to the end of line:

        if (fscanf(indice2r, "%d", &indice2) != 1) {
            /* handle invalid input... */
        }
        /* consume the end of the line */
        int c;
        while ((c = getc(indice2_r)) != EOF && c != '\n')
            continue;
chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • I don't know what the expected number of conversions is because I am supposed to add a new line every time I want to add a person to the .txt file. Thats why I put the return value to EOF. – Jeremy Stubbert Apr 28 '18 at 19:25
  • OP doesn't check `fopen` either. – Weather Vane Apr 28 '18 at 19:25
  • @JeremyStubbert please read the man page for what "number of conversions" means. In the simple case `fscanf(fptr, "%d", &indice1)` any other function value than `1` indicates a failure. – Weather Vane Apr 28 '18 at 19:27