473,597 Members | 2,145 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How read ONLY last row ASCII file

PHP code .....

<?
//Open file.
$file=fopen("fi le.txt","r");
//read file so long when it isn't end.
while (!feof($file))
{
//read and print row + LF.
$rivi=fgets($fi le,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 6894
On Feb 8, 7:31 am, "Ilkka Maatta" <ilkka.maa...@p hnet.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**********@p hnet.fiwrote in message
news:WJ******** *********@reade r1.news.saunala hti.fi...
PHP code .....

<?
//Open file.
$file=fopen("fi le.txt","r");
//read file so long when it isn't end.
while (!feof($file))
{
//read and print row + LF.
$rivi=fgets($fi le,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.ohjel mointi
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("fi le.txt","r");
while (!feof($file)){
$rivi=fgets($fi le,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_conten ts and explode just for fun:
$temp = explode("\r\n", file_get_conten ts("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***@bhg byrzcv.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
9003
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 arbitrary ascii files, to replace Yorick (which I use right now and which is really fast, but not as general as Python). The problem with scipy.io.read_array was, that it is really slow, returns errors when trying to process large files and it...
2
2035
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 with notepad. When I read the file from vb, I just have the last square. How to avoid this and remove all the squares ? Thanks for your help
9
17927
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 FileNum = FreeFile() FileOpen(FileNum, TextBox1.Text, OpenMode.Random, , , Len(Employee)) Count = 1 lstView.Items.Clear() Do While Not EOF(FileNum) FileGet(FileNum, Employee, Count)
1
3988
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
18643
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 adv.
23
2816
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 declarations etc.) FileOpen(iFileIn, sInputFile, OpenMode.Binary, OpenAccess.Read)
2
1404
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 line04 detail02 000002 line05 detail03 000003 line06 detail04 000004
22
1864
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: Bill
5
11244
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 couple areas that have ASCII text that I need to extract. At the end of the 2580 bytes, I can read the report like a standard text file. It should have CR/LF at the end of each line. What is the best way for me to read this report using C#. It is...
0
7885
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8271
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8258
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5847
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5426
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3881
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2399
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1493
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1231
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.