Bug #61523 [Opn->Nab]: First call to fgets in SplFileObject doesn't increase file pointer position
| From: | cataphract@php.net | Date: | Wed, 28 Mar 2012 13:07:37 +0000 |
| Subject: | Bug #61523 [Opn->Nab]: First call to fgets in SplFileObject doesn't increase file pointer position | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-168923@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=61523&edit=1
ID: 61523
Updated by: cataphract@php.net
Reported by: marc at lamarciana dot com
Summary: First call to fgets in SplFileObject doesn't
increase file pointer position
-Status: Open
+Status: Not a bug
Type: Bug
Package: SPL related
Operating System: Debian squeeze
PHP Version: 5.3.10
Block user comment: N
Private report: N
New Comment:
This seems correct. The first line is line 0, not 1. The error is that you're calling key()
before the first line is read. You should do it after (or just use a foreach loop).
Previous Comments:
------------------------------------------------------------------------
[2012-03-27 07:54:26] marc at lamarciana dot com
Description:
------------
Calling key() method in a new SplFileObject gives 0. This is the expected result.
If you call now fgets() method and again key() method it gives again 0 as a result. I think this is
not the expected behavior.
Subsequent calls to fgets() followed by key() method increases the result by 1 (1, 2, 3...). This is
again the expected result.
The behavior is the same with SplFileObject::READ_AHEAD flag.
More information:
http://stackoverflow.com/questions/9876999/first-call-to-fgets-in-splfileobject-doesnt-advance-file-key
Consider following text file, test.txt for the Test Script:
1
2
3
Test script:
---------------
<?php
$file = new SplFileObject('test.txt', 'r');
var_dump($file->key());
$line = $file->fgets();
var_dump($file->key());
$line = $file->fgets();
var_dump($file->key());
$line = $file->fgets();
var_dump($file->key());
Expected result:
----------------
int(0) int(1) int(2) int(3)
Actual result:
--------------
int(0) int(0) int(1) int(2)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=61523&edit=1