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

From: Date: Wed, 29 Jun 2016 17:54:55 +0000
Subject: Bug #66001 [Opn]: Problem with SplFileObject::seek()
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-201917@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
 Type:               Bug
 Package:            SPL related
 Operating System:   Win & Unix
 PHP Version:        Irrelevant
 Block user comment: N
 Private report:     N

 New Comment:

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>


Previous Comments:
------------------------------------------------------------------------
[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


Thread (6 messages)

« previous php.bugs (#201917) next »