473,762 Members | 6,675 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do i read a file line by line backwards?

Can anybody help.

I need to read a txt file backwords line by line. Can anybody help me do
this.

Thanks
Chris
Jul 17 '05 #1
8 17087
Chris <cm*@raindrop.c o.uk> wrote:
I need to read a txt file backwords line by line. Can anybody help me do
this.


Use file(). It returns an array which you can read from count($array)-1
to 0.

--

Daniel Tryba

Jul 17 '05 #2
On Mon, 05 Jan 2004 15:17:47 -0800, Chris wrote:
Can anybody help.

I need to read a txt file backwords line by line. Can anybody help me do
this.

Thanks
Chris

$foo = @file('foo.php' );
$bar = array_reverse($ foo);

foreach ($foo as $baz) {
print $baz . "\n";
}
HTH =)

Regards,

Ian

--
Ian.H [Design & Development]
digiServ Network - Web solutions
www.digiserv.net | irc.digiserv.ne t | forum.digiserv. net
Programming, Web design, development & hosting.

Jul 17 '05 #3
You can also use function *array_reverse* to reverse the array, after
calling *file*. But dont do this unnecessarily as it will put extra
load on the server.

You can even reverse a string using following code:

$str = implode("",arra y_reverse(preg_ split('//', $str)));

--
Rahul

Daniel Tryba <ne************ ****@canopus.nl > wrote in message news:<bt******* ***@news.tue.nl >...
Chris <cm*@raindrop.c o.uk> wrote:
I need to read a txt file backwords line by line. Can anybody help me do
this.


Use file(). It returns an array which you can read from count($array)-1
to 0.

Jul 17 '05 #4
jn

"Ian.H" <ia*@WINDOZEdig iserv.net> wrote in message
news:pa******** *************** ****@hybris.dig iserv.net...
On Mon, 05 Jan 2004 15:17:47 -0800, Chris wrote:
Can anybody help.

I need to read a txt file backwords line by line. Can anybody help me do
this.

Thanks
Chris

$foo = @file('foo.php' );
$bar = array_reverse($ foo);

foreach ($foo as $baz) {
print $baz . "\n";
}
HTH =)

Regards,

Ian

--
Ian.H [Design & Development]
digiServ Network - Web solutions
www.digiserv.net | irc.digiserv.ne t | forum.digiserv. net
Programming, Web design, development & hosting.


That should be:

foreach ($bar as $baz) {
print $baz . "\n";
}

Jul 17 '05 #5
Daniel Tryba <ne************ ****@canopus.nl > wrote in message news:<bt******* ***@news.tue.nl >...
Chris <cm*@raindrop.c o.uk> wrote:
I need to read a txt file backwords line by line. Can anybody help me do
this.


Use file(). It returns an array which you can read from count($array)-1
to 0.


Is their a way of doing this without reading the whole file into
memory as it could potentially be a large file?

thanks
chris
Jul 17 '05 #6
Chris wrote:

Daniel Tryba <ne************ ****@canopus.nl > wrote in message news:<bt******* ***@news.tue.nl >...
Chris <cm*@raindrop.c o.uk> wrote:
I need to read a txt file backwords line by line. Can anybody help me do
this.


Use file(). It returns an array which you can read from count($array)-1
to 0.


Is their a way of doing this without reading the whole file into
memory as it could potentially be a large file?


In this case, I would recommend something like the following (ghastly) mix of
code and pseudocode :o). I haven't tested it, so there may be a logic error -
don't treat is as gospel. Basically, you open the file and read it chunk by
chunk backwards (that is, the chunks start at the end and move towards the
beginning of the file. Each individual chunk is read forwards). Make each
chunk into an array. Unless you're reading from the beginning of the file,
remember how big the first record is and then kill it, as it's likely
incomplete. During the next loop, adjust your fseek to compensate for each
previous killed record. Then read through the array backwards and do whatever
it is you want to the records. Remember, it's good practice not to loop through
the whole thing if you don't need to. In other words, use "break" and
"continue" as appropriate.

fopen(); //open file
$intChunkSize = 5 * 1024; //set chunk size to bigger than max record size
$intAdjustment = 0;
$intSeekTo = 1;

loop //loop while $intSeekTo > 0
$intSeekTo = filesize() - ($intChunkSize * $intLoopNumber) + $intAdjustment;
if ($intSeekTo < 0){
$intChunkSize += intSeekTo; //adjust your chunk size to bytes remaining
$intSeekTo = 0;
}

fseek(); //fseek to $intSeekTo
fread(); //read $intChunkSize bytes from file

$arrFoo = split("\n", fread()); //read up to intChunksize bytes into array
$intAdjustment += strlen($arrFoo[0]); //remember how much to adjust
if ($intSeekTo > 0) //if this isn't the last loop
array_shift($ar rFoo); //kill the first (incomplete) record
for ($i = count($arrFoo)-1; $i >= 0; ++$i) //loop through array backwards
//do your thing

end loop

Regards,
Shawn
--
Shawn Wilson
sh***@glassgian t.com
http://www.glassgiant.com
Jul 17 '05 #7
"Chris" <cm*@raindrop.c o.uk> wrote in message
news:bb******** *************** ***@posting.goo gle.com...
Can anybody help.

I need to read a txt file backwords line by line. Can anybody help me do
this.

Thanks
Chris

This is just a thought,

why not just open the file, seek to the end, read byte by byte backwords,
untill you find a \n

then what you have read in is a line,

repeat for next line,

repeat for next line,

repeat for next line,

--
Mike Bradley
http://www.gzentools.com -- free online php tools
Jul 17 '05 #8
Chris wrote:
Can anybody help.

I need to read a txt file backwords line by line. Can anybody help me do
this.


Are you on Linux? If so:

$file = popen("tac $filename",'r') ;
while ($line = fgets($file,809 6) {
echo $line;
}

tac == cat backwards (lame joke but there you go)

Jul 17 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

40
61227
by: Abby | last post by:
My .dat file will contain information like below. /////////// First 0x04 0x05 0x06 Second 0x07
3
3596
by: juli jul | last post by:
Hello, I have a text file and I have to check if the line I read contains some word,the problem is how to read it in a loop and check because if I am doing something like this: while(((line=file.ReadLine())!=null)&&(line.IndexOf("abc")==-1)) I will read additional line which doesn't consit abc ,how to avoid this? Thanks a lot!
1
3678
by: kaiser | last post by:
Hello. I am trying to write a program that continues to read the last line of a txt file until a stop button is clicked. while (ContinueLoop == true ) { while ((LastLine = file.ReadLine()) != null) { LastLine = file.ReadLine(); }//while readline
35
11490
by: RyanS09 | last post by:
Hello- I am trying to write a snippet which will open a text file with an integer on each line. I would like to read the last integer in the file. I am currently using: file = fopen("f.txt", "r+"); fseek(file, -2, SEEK_END); fscanf(file, "%d", &c); this works fine if the integer is only a single character. When I get into larger numbers though (e.g. 502) it only reads in the 2. Is there
6
3554
by: Kinbote | last post by:
Hi, I'm trying to make a function that opens a file, reads it in line by line, puts each line into an malloc'd array, and returns the array. I suspect I'm going about it in an atypical fashion, as I'm avoiding the use of fscanf and fgets to read in lines. I don't want to have to specify a temporary char* buffer to read in each line, and then have to concern myself with the (remote) possibility of overflows or with increasing the buffer...
11
11377
by: jo3c | last post by:
hi everybody im a newbie in python i need to read line 4 from a header file using linecache will crash my computer due to memory loading, because i am working on 2000 files each is 8mb fileinput don't load the file into memory first how do i use fileinput module to read a specific line from a file? for line in fileinput.Fileinput('sample.txt')
3
1230
by: George | last post by:
There is a file name file.txt with content below: 11 21 31 41 41 31 21 11 31 21 11 41 How can I read the file line by line, and then print the first three columns(separed by tab)? Thanks for any idea.
6
2004
by: karodegaurav | last post by:
hi, This is gaurav karode. i want a little code help.I am making an application in which i have to show a msg box and the content of msgbox will be from a text file. so basiclly i have to read the first line of text file and show it into a msg box. i tried different things but unable to do that. ican read from the text file but how to show it into a msg box.One more thing we can use msgbox.showin windows application but cant into a web...
2
2869
by: deenar | last post by:
Hi I'm having problems with Java in reading the next line in a text file to execute to find a solution. My program is read the data from the text file and excute it and provide the answer on the screen.I've complied my program but can't get the program to read the next line in the text file. Please could someone help!!!Below is part of the code. class Trianglesf { public static void main(String args) { String input;
0
1845
by: leeamiin | last post by:
Hi, i need help with bellow file format, i work for telecom company and i'm the developer, what i need help with is to read file with bellow format, as you can see the file has { and , as separation delimiter. each line of bellow data is a cell together are one row value CallEventRecord ::= moCallRecord : { recordType moCallRecord, servedIMSI ‘64001260000010F1’H,
0
9377
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
10136
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...
1
9925
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9811
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...
0
8814
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7358
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
5266
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...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3509
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.