php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #67748 fscanf does not work when reading text file with only one character
Submitted: 2014-08-02 14:41 UTC Modified: 2014-10-06 16:18 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: tr_77019 at yahoo dot com Assigned:
Status: No Feedback Package: Unknown/Other Function
PHP Version: 5.3.28 OS: Windows
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2014-08-02 14:41 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-08-13 13:36 UTC] ab@php.net
-Status: Open +Status: Feedback
 [2014-08-13 13:36 UTC] ab@php.net
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.
 [2014-10-06 16:18 UTC] ab@php.net
-Status: Feedback +Status: No Feedback
 [2014-10-06 16:18 UTC] ab@php.net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.


 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Sat Dec 03 20:05:54 2022 UTC