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

Monday, October 31, 2022

[FIXED] How to correctly read file to EOF

 October 31, 2022     eof, php     No comments   

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)
  • 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

1,213,410

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 © 2025 PHPFixing