473,321 Members | 1,708 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

How read ONLY last row ASCII file

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
3 6877
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Sebastian Krause | last post by:
Hello, I tried to read in some large ascii files (200MB-2GB) in Python using scipy.io.read_array, but it did not work as I expected. The whole idea was to find a fast Python routine to read in...
2
by: Stan Sainte-Rose | last post by:
Hi, I need to read a kind of text file from vb.net I do know how to read a simple file, but this one seems to have special characters. Some characters appears like squares when open this file...
9
by: Zak Milas | last post by:
I have the code below: Private Sub cmdFill_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdFill.Click Dim Employee As UploadFile, Count As Integer, Temp As String...
1
by: Jose Reckoner | last post by:
I'm running python 2.3 on Windows XP. Anyone have a quick small script to convert .DT1 and .DEM data to ASCII or some other format? I don't need a viewer. Thanks!
8
by: Vijay | last post by:
Hi , I am doing a small project in c. I have a Hexadecimal file and want to convert into ascii value. (i.e., Hexadecimal to Ascii conversion from a file). Could anyone help me? Thanks in...
23
by: ShaneO | last post by:
Hello, I wish to extract embedded string data from a file using a Binary Read method. The following code sample is used in VB.NET and similar code is used in VB6 - (Assume variable...
2
by: =?Utf-8?B?YW5rMmdv?= | last post by:
Thanks in advance for reading this. Let's say I have a file (file01) with this data in ASCII (ignore line col): line01 123abc line02 Header01 Starts blah var line03 detail01 000001...
22
by: pbd22 | last post by:
Hi. I am building a custom telnet interface and my problem is that I want to read the user input along with the previously written stream. Right now I am logging the user. I have Login:...
5
by: dm3281 | last post by:
Hello, I have a text report from a mainframe that I need to parse. The report has about a 2580 byte header that contains binary information (garbage for the most part); although there are a...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.