php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #19959 fgets, fgetc
Submitted: 2002-10-17 12:07 UTC Modified: 2004-08-25 01:11 UTC
From: holliwell at gmx dot net Assigned:
Status: Closed Package: Documentation problem
PHP Version: 4.2.3 OS: win32/linux
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: holliwell at gmx dot net
New email:
PHP Version: OS:

 

 [2002-10-17 12:07 UTC] holliwell at gmx dot net
Hi,

maybe only a doc problem.
fgetc seems to be binary safe, while fgets not.

assume:
fopen a lokal file,
read it with fgetc or fgets,

you'll get bogus results with fgets if the opened file contains \0

Friedhelm Betz

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-10-17 12:35 UTC] sniper@php.net
I assume you have used 'rb' for mode in fopen..?

 [2002-10-18 12:22 UTC] holliwell at gmx dot net
yes
 [2002-10-19 10:56 UTC] iliaa@php.net
This bug has been fixed in CVS.

In case this was a PHP problem, snapshots of the sources are packaged
every three hours; this change will be in the next snapshot. You can
grab the snapshot at http://snaps.php.net/.
 
In case this was a documentation problem, the fix will show up soon at
http://www.php.net/manual/.

In case this was a PHP.net website problem, the change will show
up on the PHP.net site and on the mirror sites in short time.
 
Thank you for the report, and for helping us make PHP better.

fgets() is now binary safe in latest CVS.
 [2002-10-19 17:57 UTC] philip@php.net
Is fgetc() binary safe now too?  Only fgets() has been documented as such thus far.  fgetc() lacks documention on the matter altogether.
 [2002-11-10 18:43 UTC] philip@php.net
The first post suggests fgetc() is binary safe, anyone know since when?  Marking this as open until it's documented.
 [2003-01-20 14:20 UTC] pollita@php.net
This bug has been fixed in CVS.

In case this was a PHP problem, snapshots of the sources are packaged
every three hours; this change will be in the next snapshot. You can
grab the snapshot at http://snaps.php.net/.
 
In case this was a documentation problem, the fix will show up soon at
http://www.php.net/manual/.

In case this was a PHP.net website problem, the change will show
up on the PHP.net site and on the mirror sites in short time.
 
Thank you for the report, and for helping us make PHP better.

The meat of /php4/ext/standard/file.c::fgetc() hasn't changed since rev 1.1  It's just too simple not to be binary safe.
 [2004-08-24 10:22 UTC] wzfg at singtel dot com
Hi,frined,
I am Mr Wang Zhan Feng.
I think the bug on fgetc still existing. see my program:
test2.php:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head>

<style type="text/css">
<!--
body {  margin-left: 10px; margin-right: 10px; margin-top: 16px; margin-bottom: 16px }
-->
</style>
<!--
#!/usr/local/bin/php
-->
</head>
<body>
<?php
error_reporting(E_ALL);
$ifp=fopen("test2.php","rb");
while(true){
	$ic=fgetc($ifp);
	if($ic == false)break;
	print $ic;
}
fclose($ifp);
?>
</body>
</html>

Every time, the program run to 0, then it will stop, I tried to change to other number, then it is OK.
I am testing on PHP ver 5.0.1

Thanks.
 [2004-08-25 01:11 UTC] philip@php.net
There is a Warning inside of the fgetc() docs:

This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE, such as 0 or "". Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.

if (0 == false) echo "Yes";
http://php.net/manual/en/types.comparisons.php
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Jul 13 09:01:32 2025 UTC