|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Dec 14 19:00:01 2025 UTC |
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.