PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, October 30, 2022

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

 October 30, 2022     c, eof, file     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

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

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing