Cara
Cara

Reputation: 165

Why is the end of a file is not found, when moving the chars?

I try to output a files content, but I need to add +20 to each char, because the characters in the file are moved by -20. Outputting the content works but reaching the end of a file, Ùis printed in a loop. Can anyone help me with my code?

    while ((holder = (fgetc(dateiGlob)-ENC_NUM)) != EOF) // ENC_Num = 20
    {
        if (holder == ';') // Replace ; 
        {
            printf("\n");
            holder = '\0';
        }

        if (holder == '/') // Replace /
        {
            printf(" | Anmerkung: ");
            holder = '\0';
        }

        putchar(holder); // Print in stdout
    }

    printf("\n");
}

When ENC_NUM is set to 0, EOF is found again. What do I need to change? Thank you!

Upvotes: 0

Views: 50

Answers (1)

Martin Heralecký
Martin Heralecký

Reputation: 5779

Look carefuly at your while condition.

(fgetc(dateiGlob) - ENC_NUM) != EOF

If you set ENC_NUM = 0, the condition becomes fgetc(dateiGlob) != EOF, which is what you need in order to stop reading the file when you're at its end.

Now, if you want to change the left side of the condition (by making ENC_NUM a non-zero integer), you also need to change its right side in the same way (in order to preserve the logic):

(fgetc(dateiGlob) - ENC_NUM) != (EOF - ENC_NUM)
//                ^^^^^^^^^          ^^^^^^^^^

Upvotes: 2

Related Questions