Bug #62773 [Opn->Nab]: fgets() is not binary safe
| From: | aharvey@php.net | Date: | Wed, 08 Aug 2012 02:47:59 +0000 |
| Subject: | Bug #62773 [Opn->Nab]: fgets() is not binary safe | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-8714@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=62773&edit=1
ID: 62773
Updated by: aharvey@php.net
Reported by: scott at smashcat dot org
Summary: fgets() is not binary safe
-Status: Open
+Status: Not a bug
Type: Bug
Package: Documentation problem
Operating System: Linux
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
fgets() is binary safe, as the documentation says. However, you've given an explicit length,
and to quote the manual:
"Returns a string of up to length - 1 bytes read from the file pointed to by handle. If there
is no more data to read in the file pointer, then FALSE is returned."
fgets($F, 6) returns a 5 byte string, which can't be unpacked because unpack() is expecting 6
bytes per the format.
Previous Comments:
------------------------------------------------------------------------
[2012-08-07 23:23:52] scott at smashcat dot org
Description:
------------
fgets() does not load the binary data for unpack to process. If the fgets() function is replaced
with fread() then the example below works as expected. The current documentation claims that fgets()
is binary safe, which appears to be incorrect.
Test script:
---------------
<?php
print "Saving...\n";
$F=fopen('1.evid','wb');
fwrite($F,pack('C6',65,66,67,68,69,70));
fclose($F);
print "Loading...\n";
$F=fopen('1.evid','rb');
$d=unpack('C6',fgets($F,6));
print "Result: ".print_r($d,1)."\n";
print count($d)."\n";
fclose($F);
Expected result:
----------------
It should display an array with 6 values, as written to the file.
Actual result:
--------------
No data is read, the array is empty.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=62773&edit=1