Bug #46569 [Ver->Dup]: SplFileObject: fgetcsv after seek returns wrong line
| From: | cmb@php.net | Date: | Wed, 18 Nov 2020 12:46:25 +0000 |
| Subject: | Bug #46569 [Ver->Dup]: SplFileObject: fgetcsv after seek returns wrong line | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-230427@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=46569&edit=1
ID: 46569
Updated by: cmb@php.net
Reported by: duke at masendav dot com
Summary: SplFileObject: fgetcsv after seek returns wrong line
-Status: Verified
+Status: Duplicate
Type: Bug
Package: SPL related
Operating System: Macos 10.5
PHP Version: 5.2.6
-Assigned To:
+Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
Indeed, this is not particularly related to fgetcsv(), but rather
a duplicate of bug #62004.
Previous Comments:
------------------------------------------------------------------------
[2019-12-12 09:29:44] jeremy dot touati at gmail dot com
Note that this doesn't seem related specifically to fgetcsv.
It's just that SplFileObject::seek appears to set the file pointer position differently between
the first line and the other ones.
Example (still valid in PHP 7.4):
https://3v4l.org/O89dJ
------------------------------------------------------------------------
[2019-03-20 09:48:36] tomme87 at gmail dot com
Still an issue with PHP 7.3
------------------------------------------------------------------------
[2018-03-29 23:34:41] cmb@php.net
Still unresolved: <https://3v4l.org/3Uh2L>.
------------------------------------------------------------------------
[2017-10-28 23:48:06] sylvain dot witmeyer at gmail dot com
9 years later this bug isn't solved so here is a snippet to achieve the same
function readBigCsv($path, $skip=1)
{
$file = new \SplFileObject($path, 'r');
$file->setFlags(\SplFileObject::READ_CSV);
$file->seek($skip);
while (!$file->eof()){
yield $file->current();
$file->next();
}
}
------------------------------------------------------------------------
[2014-01-12 11:50:26] asylow at free dot fr
If think the cases above (not using seek) are related to this bug
PHP 5.4.7
Case 1 is Wrong
-----
$file = new SplFileObject('file.csv', 'r');
echo $file->current();
var_dump($file->fgetcsv());
echo $file->current();
var_dump($file->fgetcsv());
-----
Result :
First array(1) { [0]=> string(6) "Second" }
Second array(1) { [0]=> string(5) "Third" }
Case 2 is OK
-----
$file = new SplFileObject('file.csv', 'r');
var_dump($file->fgetcsv());
echo $file->current();
var_dump($file->fgetcsv());
echo $file->current();
-----
Result :
array(1) { [0]=> string(5) "First" } First
array(1) { [0]=> string(6) "Second" } Second
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=46569
--
Edit this bug report at https://bugs.php.net/bug.php?id=46569&edit=1