Bug #66001 [Opn->Dup]: Problem with SplFileObject::seek()

From: Date: Wed, 18 Nov 2020 13:14:25 +0000
Subject: Bug #66001 [Opn->Dup]: Problem with SplFileObject::seek()
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-230430@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=66001&edit=1 ID: 66001 Updated by: cmb@php.net Reported by: miichal dot kozlowski at gmail dot com Summary: Problem with SplFileObject::seek() -Status: Open +Status: Duplicate Type: Bug Package: SPL related Operating System: Win & Unix PHP Version: Irrelevant -Assigned To: +Assigned To: cmb Block user comment: N Private report: N New Comment: Well, actually this is a duplicate of bug #62004. Previous Comments: ------------------------------------------------------------------------ [2020-01-15 16:24:41] wowlinuxdebian at gmail dot com Same here, as maxlogic: seek(0) then fgetcsv() - reading from 1st line seek(1) then fgetcsv() - reading from 3rd line how I can seek to line 2? ------------------------------------------------------------------------ [2019-05-06 06:56:27] maxlogic dot ga at gmail dot com Short description: seek(0) then fgetcsv() - reading from 1st line seek(1) then fgetcsv() - reading from 3rd line seek(2) then fgetcsv() - reading from 4th line So no way to reach 2nd line using seek. I need to implement some "elegant" solution to avoid this bug. ------------------------------------------------------------------------ [2016-06-30 12:29:49] miichal dot kozlowski at gmail dot com Yes you are right, the problem is when you are working on a file and you wish to rerun the program starting from the first line. In such case you are not able to use SplFileObject::current(). That was my case, I wrote a pice of script which needed to rerun the same file from the place which you marked. ------------------------------------------------------------------------ [2016-06-29 17:54:54] cmb@php.net After calling SplFileObject::seek() the following line is already read, and available via SplFileObject::current(), see example #1[1]. However, given that your first example works, the behavior appears to be indeed inconsistent. By the way, the issue is not particularly related to ::fgetcsv(); fgets() shows the same behavior. [1] <http://php.net/manual/en/splfileobject.seek.php#refsect1-splfileobject.seek-examples> ------------------------------------------------------------------------ [2013-10-30 12:10:10] miichal dot kozlowski at gmail dot com Description: ------------ Hi, Example 1 shows we set $file->seek(0) and it will return 3 rows: Array ( [0] => 0DATA [1] => 1 St 1 Av [2] => [3] => test [4] => FL [5] => 34221 ) Array ( [0] => 1DATA [1] => 1 St 1 Av [2] => [3] => Test [4] => FL [5] => 34221 ) Array ( [0] => 2DATA [1] => 1 St 1 Av [2] => [3] => Test [4] => FL [5] => 34221 ) Example 2 shows if we set $file->seek(1) then we are expecting to get 2 rows: Array ( [0] => 1DATA [1] => 1 St 1 Av [2] => [3] => Test [4] => FL [5] => 34221 ) Array ( [0] => 2DATA [1] => 1 St 1 Av [2] => [3] => Test [4] => FL [5] => 34221 ) ,but we received 1. Array ( [0] => 2DATA [1] => 1 St 1 Av [2] => [3] => Test [4] => FL [5] => 34221 ) Test script: --------------- <?php file_put_contents('file.csv', '"0DATA";"1 St 1 Av";"";"test";"FL";"34221"' . PHP_EOL .' "1DATA";"1 St 1 Av";"";"Test";"FL";"34221"' . PHP_EOL .' "2DATA";"1 St 1 Av";"";"Test";"FL";"34221"'); //example 1 $file = new SplFileObject('file.csv'); $file->seek(0); while(!$file->eof()) { print_r($file->fgetcsv(";")); } echo '---' . PHP_EOL; //example 2 $file = new SplFileObject('file.csv'); $file->seek(1); while(!$file->eof()) { print_r($file->fgetcsv(";")); } Expected result: ---------------- Array ( [0] => 0DATA [1] => 1 St 1 Av [2] => [3] => test [4] => FL [5] => 34221 ) Array ( [0] => 1DATA [1] => 1 St 1 Av [2] => [3] => Test [4] => FL [5] => 34221 ) Array ( [0] => 2DATA [1] => 1 St 1 Av [2] => [3] => Test [4] => FL [5] => 34221 ) example 2 Array ( [0] => 1DATA [1] => 1 St 1 Av [2] => [3] => Test [4] => FL [5] => 34221 ) Array ( [0] => 2DATA [1] => 1 St 1 Av [2] => [3] => Test [4] => FL [5] => 34221 ) Actual result: -------------- Array ( [0] => 0DATA [1] => 1 St 1 Av [2] => [3] => test [4] => FL [5] => 34221 ) Array ( [0] => 1DATA [1] => 1 St 1 Av [2] => [3] => Test [4] => FL [5] => 34221 ) Array ( [0] => 2DATA [1] => 1 St 1 Av [2] => [3] => Test [4] => FL [5] => 34221 ) example 2 Array ( [0] => 2DATA [1] => 1 St 1 Av [2] => [3] => Test [4] => FL [5] => 34221 ) ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=66001&edit=1

« previous php.bugs (#230430) next »