I don't have the option of editing the text files to work right. I want
to be able to read an arbitrary text file line by line and process each
one. So I suppose I could do this (?)
<?php
$fd = fopen ("test","r");
while (!feof ($fd)) {
$buffer = fgets($fd, 4096);
if($buffer == '') {
// no \n, must be EOF
break;
}
echo "buffer is $buffer";
}
fclose ($fd);
?>
DL Neil wrote:
> Hi Monte,
>
>
>>Hi, I have a question about fgets(), it seems to pick up an extra line
>>at the end of a text file.
>
> ...
>
>>8\n
>
> ...
>
>>Here is the output (showing newlines as \n):
>>buffer is 1\n
>>buffer is 2\n
>>buffer is 3\n
>>buffer is 4\n
>>buffer is 5\n
>>buffer is 6\n
>>buffer is 7\n
>>buffer is 8\n
>>buffer is
>>My question, why is there an extra line? Or in other words, how do I get
>>this to loop exactly 8 times if there are only 8 lines?
>
>
>
> The answer is that there isn't an 'extra' line, there are as many as you
> put/PHP reads!
> The question to ask is: how does a stream file end?
> Answer: with an EOF character (not an LF).
>
> Lines in a (*nix) stream file are separated by LF or \n character, therefore
> there is a ninth 'line' (of absolutely nothing) between the last LF and the
> EOF. Take out that last \n and things should work the way you want.
>
> Regards,
> =dn
>
>
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php