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

From: Date: Thu, 30 Jun 2016 12:29:49 +0000
Subject: Bug #66001 [Opn]: Problem with SplFileObject::seek()
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-201938@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 User updated by: miichal dot kozlowski at gmail dot com Reported by: miichal dot kozlowski at gmail dot com Summary: Problem with SplFileObject::seek() Status: Open Type: Bug Package: SPL related Operating System: Win & Unix PHP Version: Irrelevant Block user comment: N Private report: N New Comment: 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. Previous Comments: ------------------------------------------------------------------------ [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 (#201938) next »