tags:

views:

113

answers:

4

Using PHP, it's possible to read off the contents of a file using fopen and fgets. Each time fgets is called, it returns the next line in the file.

How does fgets know what line to read? In other words, how does it know that it last read line 5, so it should return the contents of line 6 this time? Is there a way for me to access that line-number data?

(I know it's possible to do something similar by reading the entire contents of the file into an array with file, but I'd like to accomplish this with fopen.)

+6  A: 

There is a "position" kept in memory for each file that is opened ; it is automatically updated each time you are reading a line/character/whatever from the file.

You can get this position with ftell, and modify it with fseek :

ftell — Returns the current position of the file read/write pointer

fseek — Seeks on a file pointer

You can also use rewind to... rewind... the position of that pointer.


This is not getting you a position as a line number, but closer to a position as a character number (actually, you are getting the position as a number of bytes from the beginning of the file) ; when you have that, reading a line is just a metter of reading characters until yu hit an end of line character.


BTW : as far as I remember, these functions are coming from the C language -- PHP itself being written in C ;-)

Pascal MARTIN
+3  A: 

Files are just a stream of data, read from the beginning to the end. The OS will remember the position you've read so far in that file. If needed, doing so in the application as well is fairly simple. The OS only cares about byte positions though, not lines.

Just imagine dealing out a deck of 52 card sequentially. You hand off the first card. Next time the 2. card. When you want to give out the 3. card , you don't need to start counting from the start again, or even remembering where you were you just hand out the next available card, and that'll be the third.

It might be a bit more work that's needed to read lines, since you'd want to buffer data read from the actual file for preformance sake, but it's not that much more to it than to record the offset of the last piece of data you handed out, find the next newline character and hand off all the data between those 2 points.

PHP nor the OS has no real need to keep the line number around, since all the system care about is "next line". If you want to know the line number, you keep a counter and increment it every time your app reads a line.

$lineno=0;
while (!feof($handle)) {
    $buffer = fgets($handle, 4096);
    lineno++; // keep track of the line number
     ...

}
nos
good example. @stalepretzel fgets() returns a new line, so you can just count each line it returns.
bucabay
A: 

You could just call fgets and increment a var $line_number each time you call it. That would tell you the line it is on.

sobertillnoon
A: 

i hav this old sample i hob its can help you :)

$File = file('path');
$array = array();

$linenr = 5;

foreach( $File AS $line_num => $line )
{
$array = array_push( $array , $line );
}

echo $array[($linenr-1)];
NeoNmaN
this seems to work :)
william
It works, but I specifically asked for a solution that used fopen(), not file(). :o
stalepretzel
the defrient its not much between fopen and file, only if you file you get are over 10mb, :) and file are easyere to use.
NeoNmaN