Sunday, October 30, 2022

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

Issue

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!


Solution

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)
//                ^^^^^^^^^          ^^^^^^^^^


Answered By - Martin Heralecký
Answer Checked By - David Goodson (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.