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
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
27 - 6 = ?
Subscribe to this entry?

 
 [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-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 06:01:29 2024 UTC