Monday, October 31, 2022

[FIXED] How to correctly read file to EOF

Issue

I have a function that reads comments in a text file. Comment can last to end of line, if there is no end of line it lasts to end of file. My problem is I can't find out how to test it whether the end of file was reached. At first I had this:

while($char != "\n" && $char != false){
    $char = fgetc($inputFile);
}

If there was 0 in a comment it ended the loop which I didn't want to. Then I tried this:

while($char != "\n" && !feof($inputFile)){ 
    $char = fgetc($inputFile);
}

This broke up the whole program. I tried to google something but feof and != false is all I found.


Solution

Use !== to check for FALSE:

while($char != "\n" && $char !== FALSE){
    $char = fgetc($inputFile);
}


Answered By - Bart Friederichs
Answer Checked By - Candace Johnson (PHPFixing Volunteer)

No comments:

Post a Comment

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