stream_get_line reports two lines instead of one
| Bug #60455 | stream_get_line reports two lines instead of one | ||||
|---|---|---|---|---|---|
| Submitted: | 2011-12-07 10:01 UTC | Modified: | 2012-09-01 16:15 UTC | ||
| From: | jakub dot lopuszanski at nasza-klasa dot pl | Assigned: | cataphract (profile) | ||
| Status: | Closed | Package: | Streams related | ||
| PHP Version: | Irrelevant | OS: | linux | ||
| Private report: | No | CVE-ID: | None | ||
[2011-12-07 10:01 UTC] 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) ""
Patches
Pull Requests
History
AllCommentsChangesGit/SVN commits
[2011-12-11 21:08 UTC] cataphract@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: cataphract
[2012-03-05 01:55 UTC] stas@php.net
-Status: Closed +Status: Re-Opened
[2012-03-05 01:55 UTC] stas@php.net