Phpfeof function usage and precautions

Source: Internet
Author: User
The eof () function checks whether the object has reached the end of the object (eof). If the object pointer to EOF or an error occurs, TRUE is returned. Otherwise, an error (including socket timeout) is returned. In other cases

The eof () function checks whether it has reached the end of the file (eof ).

TRUE is returned if the file pointer reaches EOF or when an error occurs. Otherwise, an error (including socket timeout) is returned. otherwise, FALSE is returned.

Syntax:Feof (file)

Parameter description

File is required, specifying the file to be checked.

Note:The file parameter is a file pointer, which must be valid and must point to a file successfully opened (but not closed) by fopen () or fsockopen.

  1. $ File = fopen ("test.txt", "r ");
  2.  
  3. // Output all rows in the text until the end of the file.
  4. While (! Feof ($ file ))
  5. {
  6. Echo fgets ($ file )." ";
  7. }
  8.  
  9. Fclose ($ file );
  10. ?>
  11.  
  12. If (file_exists ($ pmr_config ["datasetfile"]) {
  13. $ Tmp_counter = 0;
  14. $ Hd = fopen ($ pmr_config ["datasetfile"], "r ");
  15. If ($ hd! = FALSE ){
  16. While (! Feof ($ hd )){
  17. $ Buffer = fgets ($ hd );
  18. If ($ tmp_counter >=$ seq ){
  19. $ Result [] = $ buffer;
  20. }
  21. $ Tmp_counter ++;
  22.  
  23. If ($ tmp_counter >=$ seq + $ size ){
  24. Break;
  25. }
  26. }
  27. } Else {
  28. Echo "warning: open file {$ pmr_config [" datasetfile "]} failed! PHP_EOL ";
  29. }
  30. } Else {
  31. Echo "warning: file {$ pmr_config [" datasetfile "]} does not exsits! PHP_EOL ";
  32. }

When the number of read rows includes the end of the file, the $ result array always has one more element than expected:

(Boolean) false

In other words, if the last row is read, the feof function returns TRUE, and the while loop exits. why not?

1

While (! Feof ($ hd )){

It turns out to be like this:

  1. // If file can not be read or doesn't exist fopen function returns FALSE
  2. $ File = @ fopen ("no_such_file", "r ");
  3.  
  4. // FALSE from fopen will issue warning and result in infinite loop here
  5. While (! Feof ($ file )){
  6. }
  7.  
  8. Fclose ($ file );
  9. ?>

Feof () is, in fact, reliable. However, you have to use it carefully in conjunction with fgets (). A common (but incorrect) approach is to try something like this:

  1. $ Fp = fopen ("myfile.txt", "r ");
  2. While (! Feof ($ fp )){
  3. $ Current_line = fgets ($ fp );
  4. // Do stuff to the current line here
  5. }
  6. Fclose ($ fp );
  7. ?>

Tips and notes:

Tip:The feof () function is useful for traversing data with unknown lengths.

Note:If the server does not close the connection opened by fsockopen (), feof () will wait until it times out and return TRUE. the default timeout limit is 60 seconds. you can use stream_set_timeout () to change the value.

Note:If the passed file pointer is invalid, it may be in an infinite loop, because EOF does not return TRUE.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.