Bug #67748 [Opn->Fbk]: fscanf does not work when reading text file with only one character
| From: | ab@php.net | Date: | Wed, 13 Aug 2014 13:36:44 +0000 |
| Subject: | Bug #67748 [Opn->Fbk]: fscanf does not work when reading text file with only one character | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-187100@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=67748&edit=1
ID: 67748
Updated by: ab@php.net
Reported by: tr_77019 at yahoo dot com
Summary: fscanf does not work when reading text file with
only one character
-Status: Open
+Status: Feedback
Type: Bug
Package: Unknown/Other Function
Operating System: Windows
PHP Version: 5.3.28
Block user comment: N
Private report: N
New Comment:
I can't reproduce this, running this
$fd = fopen('numbers.txt', 'r');
var_dump(fscanf($fd, '%d'));
and using a file with one number per line.
Could you try to remove the newline check from the format, and pls check the new lines in the file.
Thanks.
Previous Comments:
------------------------------------------------------------------------
[2014-08-02 14:41:38] tr_77019 at yahoo dot com
Description:
------------
fscanf does not read the first line of a text file when there is only one integer on each line.
However, it does work for characters and strings.
Test script:
---------------
<?php
$fp = fopen("numbers.txt", "r");
while ($num = fscanf($fp, "%d\n")) {
list($newnum) = $num;
echo "This is $newnum<br>";
}
fclose($fp);
?>
numbers.txt contains:
1
2
3
4
It works for characters and strings:
<?php
$fp = fopen("names.txt", "r");
while ($name = fscanf($fp, "%s\n")) {
list($newname) = $name;
echo "This is $newname<br>";
}
fclose($fp);
?>
names.txt contains:
javier
hiroshi
robert
luigi
or:
a
b
c
d
Expected result:
----------------
This is 1
This is 2
This is 3
This is 4
This is javier
This is hiroshi
This is robert
This is luigi
This is a
This is b
This is c
This is d
Actual result:
--------------
This is
This is 2
This is 3
This is 4
This is javier
This is hiroshi
This is robert
This is luigi
This is a
This is b
This is c
This is d
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=67748&edit=1