Home > Archive > PHP Language > May 2004 > Read file knowing PRECISE end of each line
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Read file knowing PRECISE end of each line
|
|
| Alberto 2004-05-19, 12:30 pm |
| Hallo
I'd like to parse a file but knowing where EXACTLY the end of each
line is (that is: at which byte, included), so to browse it line by
line indeed.
I don't want to use file() that seems to load the whole file, and at
the same time I'd like to know whether there is a less "arbitrary"
way to read a file line by line than by just passing as argument a
length in bytes which, being a number I'd _assume_ as fit, might well
be LESS than a whole line.
I'd really need to be sure I'm reading line by line, grabbinb each
time the whole line; but without keeping the server memory too
clustered with file()
thank you in advance
Alberto
| |
|
| On Wed, 19 May 2004 17:08:43 +0200, Alberto <NOSPAM@hotmail.com> wrote:
> Hallo
>
> I'd like to parse a file but knowing where EXACTLY the end of each
> line is (that is: at which byte, included), so to browse it line by
> line indeed.
>
> I don't want to use file() that seems to load the whole file, and at
> the same time I'd like to know whether there is a less "arbitrary"
> way to read a file line by line than by just passing as argument a
> length in bytes which, being a number I'd _assume_ as fit, might well
> be LESS than a whole line.
>
> I'd really need to be sure I'm reading line by line, grabbinb each
> time the whole line; but without keeping the server memory too
> clustered with file()
>
> thank you in advance
> Alberto
>
>
Loading the entire thing is the only way you can deal with files, the only
way to know the contents of the file is to read the contents of that file,
completely. Maybe you should consider moving over to a database solution,
whereby you can scan indexes without content?
When you load a file into the memory, you can just refer to lines easily,
$row = file('toot.txt');
if I wanted to print the fifth line:
echo $row[4];
Hope that helps.
--
If we can't play God, who will?
| |
| Andy Hassall 2004-05-19, 4:30 pm |
| On Wed, 19 May 2004 17:08:43 +0200, "Alberto" <NOSPAM@hotmail.com> wrote:
>I'd like to parse a file but knowing where EXACTLY the end of each
>line is (that is: at which byte, included), so to browse it line by
>line indeed.
>
>I don't want to use file() that seems to load the whole file, and at
>the same time I'd like to know whether there is a less "arbitrary"
>way to read a file line by line than by just passing as argument a
>length in bytes which, being a number I'd _assume_ as fit, might well
>be LESS than a whole line.
Not really. To find whether you're at the end of a line you have to read from
the file (obviously). You either do it in one go with file() [bad if the file
could be big], or byte by byte checking for newlines [generally inefficient to
read such small amounts], or you read in arbitrary chunks with fread or similar
and split up each chunk into lines, saving any leftovers to prepend to the next
chunk.
--
Andy Hassall <andy@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
| |
| Alberto 2004-05-19, 9:30 pm |
| Thank you all, I actually found the solution on a group, I provide the
answer here in case others find it useful:
Mmmh I noticed now:
-----
As of PHP 4.3, omitting length will keep reading from the stream
until it reaches the end of the line.
-------
I don't run php 4.3 but 4.0 so I was still unaware of that. Well, at
least glad that the problem I was trying to figure out has been
sensed as analogously "haunting" by Php developers to the degree they
have now inserted a default built in behaviour that meets precisely
this need!
I suppose now I just have to upgrade my php engine!
thank you a lot, these funcs, even the most traditionsl ones, at
times change at every new subversion ;)
ciao
Alberto
> Please... ;)
>
> http://www.php.net/manual/en/function.fgets.php
| |
| Chris Hope 2004-05-19, 9:30 pm |
| Alberto wrote:
> thank you a lot, these funcs, even the most traditionsl ones, at
> times change at every new subversion ;)
Yeah it's funny how when you've been using a programming language for a few
years you get used to the old behavior of a function and never bother to
refer back to the manual page again for it once you've used it a lot. I had
no idea you no longer need to specify the number of bytes to read and I
must have used that function hundreds of times over the years :)
Note from the manual though in 4.2.x that if you don't specify a length it
defaults to 1024. It's only from 4.3.0 that it reads the whole line.
--
Chris Hope
The Electric Toolbox - http://www.electrictoolbox.com/
|
|
|
|
|