From:             
Operating system: linux
PHP version:      Irrelevant
Package:          Streams related
Bug Type:         Bug
Bug description:stream_get_line reports two lines instead of one

Description:
------------
If a file consists of single line, and you use a straightforward loop over
all 
lines using stream_get_line, you may be suprised that there are actually
two 
lines, where the second one is empty string.

This is inconsistent with the case where you actually have two lines in a
file, 
and stream_get_line reports two lines as expected without the additional
empty 
third line.

This inconsistency makes it quite difficult to write a correct code.

Test script:
---------------
I have three files:

lopuszanski@vanisoft:~$ hexdump -C one_line.txt
00000000  61 0a                                             |a.|
00000002
lopuszanski@vanisoft:~$ hexdump -C two_lines.txt
00000000  61 0a 62 0a                                       |a.b.|
00000004
lopuszanski@vanisoft:~$ hexdump -C two_lines_one_of_which_is_empty.txt
00000000  61 0a 0a                                          |a..|
00000003

And the following script:
lopuszanski@vanisoft:~$ cat test.php
<?php
while(!feof(STDIN)){
  $line = stream_get_line(STDIN,1000000,"\n");
  var_dump($line);
}
?>


Expected result:
----------------
lopuszanski@vanisoft:~$ php test.php < one_line.txt
string(1) "a"
lopuszanski@vanisoft:~$ php test.php < two_lines.txt
string(1) "a"
string(1) "b"
lopuszanski@vanisoft:~$ php test.php < two_lines_one_of_which_is_empty.txt
string(1) "a"
string(0) ""


Actual result:
--------------
lopuszanski@vanisoft:~$ php test.php < one_line.txt
string(1) "a"
string(0) ""
lopuszanski@vanisoft:~$ php test.php < two_lines.txt
string(1) "a"
string(1) "b"
lopuszanski@vanisoft:~$ php test.php < two_lines_one_of_which_is_empty.txt
string(1) "a"
string(0) ""


-- 
Edit bug report at https://bugs.php.net/bug.php?id=60455&edit=1
-- 
Try a snapshot (PHP 5.4):            
https://bugs.php.net/fix.php?id=60455&r=trysnapshot54
Try a snapshot (PHP 5.3):            
https://bugs.php.net/fix.php?id=60455&r=trysnapshot53
Try a snapshot (trunk):              
https://bugs.php.net/fix.php?id=60455&r=trysnapshottrunk
Fixed in SVN:                        
https://bugs.php.net/fix.php?id=60455&r=fixed
Fixed in SVN and need be documented: 
https://bugs.php.net/fix.php?id=60455&r=needdocs
Fixed in release:                    
https://bugs.php.net/fix.php?id=60455&r=alreadyfixed
Need backtrace:                      
https://bugs.php.net/fix.php?id=60455&r=needtrace
Need Reproduce Script:               
https://bugs.php.net/fix.php?id=60455&r=needscript
Try newer version:                   
https://bugs.php.net/fix.php?id=60455&r=oldversion
Not developer issue:                 
https://bugs.php.net/fix.php?id=60455&r=support
Expected behavior:                   
https://bugs.php.net/fix.php?id=60455&r=notwrong
Not enough info:                     
https://bugs.php.net/fix.php?id=60455&r=notenoughinfo
Submitted twice:                     
https://bugs.php.net/fix.php?id=60455&r=submittedtwice
register_globals:                    
https://bugs.php.net/fix.php?id=60455&r=globals
PHP 4 support discontinued:          
https://bugs.php.net/fix.php?id=60455&r=php4
Daylight Savings:                    https://bugs.php.net/fix.php?id=60455&r=dst
IIS Stability:                       
https://bugs.php.net/fix.php?id=60455&r=isapi
Install GNU Sed:                     
https://bugs.php.net/fix.php?id=60455&r=gnused
Floating point limitations:          
https://bugs.php.net/fix.php?id=60455&r=float
No Zend Extensions:                  
https://bugs.php.net/fix.php?id=60455&r=nozend
MySQL Configuration Error:           
https://bugs.php.net/fix.php?id=60455&r=mysqlcfg

Reply via email to