By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,633 Members | 1,670 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,633 IT Pros & Developers. It's quick & easy.

How read ONLY last row ASCII file

P: n/a
PHP code .....

<?
//Open file.
$file=fopen("file.txt","r");
//read file so long when it isn't end.
while (!feof($file))
{
//read and print row + LF.
$rivi=fgets($file,1024);
print "$row<BR>";
}
//Close file.
fclose($file);
?>

.....is read file(ascii) and print it display.

How i can read and print only last row - don't all row?
Feb 8 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Feb 8, 7:31 am, "Ilkka Maatta" <ilkka.maa...@phnet.fiwrote:
>
How i can read and print only last row - don't all row?
If you are in a *nix system, you can do:

$lastLine = exec('tail -1 getCodeGetText.php');

Another option, much more memory intensive (for large files), but
works on any system:

$lines = file('/path/to/filename.txt');
$lastLine = $lines[(count($lines) - 1)];

Or, you could loop the lines like you are doing already and just not
print out the lines;
the last line will be the last iteration through the loop.

while (!feof($file))
$line = fgets($fp,1024);

print $line;

HTH
-Kurt

Feb 8 '07 #2

P: n/a
"Ilkka Maatta" <il**********@phnet.fiwrote in message
news:WJ*****************@reader1.news.saunalahti.f i...
PHP code .....

<?
//Open file.
$file=fopen("file.txt","r");
//read file so long when it isn't end.
while (!feof($file))
{
//read and print row + LF.
$rivi=fgets($file,1024);
print "$row<BR>";
}
//Close file.
fclose($file);
?>

....is read file(ascii) and print it display.

How i can read and print only last row - don't all row?

WTF? you asked the same stupid question already in sfnet.atk.ohjelmointi
some time ago and you got 3 replies explaing how. How many times must this
be explained to you?

1) a single line, the unoptimized easy way:
echo array_pop(file(("file.txt"));

2) modifying your code:
$file=fopen("file.txt","r");
while (!feof($file)){
$rivi=fgets($file,1024);
}
print "$row<BR>";
fclose($file);

3) the real hard-core *nix way:
passthru("tail -n 1 file.txt");
// This one really reads just the last line,
// not all the lines before it. KEWL!

4) using file_get_contents and explode just for fun:
$temp = explode("\r\n",file_get_contents("file.txt"));
echo $temp[sizeof($temp)];
--
"Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
sp**@outolempi.net | rot13(xv***@bhgbyrzcv.arg)
Feb 8 '07 #3

P: n/a
Ur guys,

Have we read http://uk.php.net/manual/en/function.fseek.php ?

If you read the comments, there are some nice functions there

This one looks good:
<?php
function readLastLine ($file) {
$fp = @fopen($file, "r");

$pos = -1;
$t = " ";
while ($t != "\n") {
if (!fseek($fp, $pos, SEEK_END)) {
$t = fgetc($fp);
$pos = $pos - 1;
} else {
rewind($fp);
break;
}
}
$t = fgets($fp);
fclose($fp);
return $t;
}
?>

Feb 8 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.