Bug #46569 [Com]: SplFileObject: fgetcsv after seek returns wrong line

From: Date: Sat, 28 Oct 2017 23:48:12 +0000
Subject: Bug #46569 [Com]: SplFileObject: fgetcsv after seek returns wrong line
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-212352@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 Comment by: sylvain dot witmeyer at gmail dot com Reported by: duke at masendav dot com Summary: SplFileObject: fgetcsv after seek returns wrong line Status: Open Type: Bug Package: SPL related Operating System: Macos 10.5 PHP Version: 5.2.6 Block user comment: N Private report: N New Comment: 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(); } } Previous Comments: ------------------------------------------------------------------------ [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 ------------------------------------------------------------------------ [2008-11-15 00:23:57] felipe@php.net It's due the check in spl_filesystem_file_read(): "long line_add = (intern->u.file.current_line || intern->u.file.current_zval) ? 1 : 0;" I don't know the reason of this checking. Etienne? ------------------------------------------------------------------------ [2008-11-14 22:53:11] jordan dot raub at dataxltd dot com I have the same issue w/ 5.2.6 when writing. <?php $fobject = new SplFileObject('/tmp/test', 'w'); $fobject->fwrite("111111111111111\n"); $fobject->fwrite("222222222222222\n"); $fobject->fwrite("333333333333333\n"); $fobject->fwrite("444444444444444\n"); $fobject->seek(2); $fobject->fwrite("555555555555555\n"); $fobject->fwrite("666666666666666\n"); $fobject->fwrite("777777777777777\n"); $fobject->fwrite("888888888888888\n"); foreach(new SplFileObject('/tmp/test', 'r') as $line) { echo $line; } ?> Expected result: ---------------- 111111111111111 222222222222222 555555555555555 666666666666666 777777777777777 888888888888888 Actual result: -------------- 555555555555555 666666666666666 777777777777777 888888888888888 ------------------------------------------------------------------------ [2008-11-13 21:57:35] duke at masendav dot com Description: ------------ Using fgetcsv() after seek()-ing to a non-zero position returns the contents of wrong line - off by one. Note that seek(0) works as expected. Also, using current() instead of fgetcsv() gives the correct line. Source file (5 lines) first,line second,line third,line fourth,line fifth,line Reproduce code: --------------- <?php $file = new SplFileObject('lines.csv'); $file->seek(1); print_r($file->fgetcsv()); Expected result: ---------------- Array ( [0] => second [1] => line ) Actual result: -------------- Array ( [0] => third [1] => line ) ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=46569&edit=1

« previous php.bugs (#212352) next »