Bug #60455 [Csd]: stream_get_line reports two lines instead of one
| From: | cataphract@php.net | Date: | Sun, 11 Dec 2011 21:08:51 +0000 |
| Subject: | Bug #60455 [Csd]: stream_get_line reports two lines instead of one | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-165809@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=60455&edit=1
ID: 60455
Updated by: cataphract@php.net
Reported by: jakub dot lopuszanski at nasza-klasa dot pl
Summary: stream_get_line reports two lines instead of one
Status: Closed
Type: Bug
Package: Streams related
Operating System: linux
PHP Version: Irrelevant
Assigned To: cataphract
Block user comment: N
Private report: N
New Comment:
Fixed in SVN. Thanks.
Previous Comments:
------------------------------------------------------------------------
[2011-12-11 21:07:57] cataphract@php.net
Automatic comment from SVN on behalf of cataphract
Revision: http://svn.php.net/viewvc/?view=revision&revision=320876
Log: - Fixed bug #60455: stream_get_line misbehaves if EOF is not detected together
with the last read.
------------------------------------------------------------------------
[2011-12-07 10:01:42] jakub dot lopuszanski at nasza-klasa dot pl
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 this bug report at https://bugs.php.net/bug.php?id=60455&edit=1