473,785 Members | 2,423 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Open text file from the from the end backwards

I'm trying to do a simple task but can't seem to find a solution. How
do I read lines from a text file backwards.

i.e. I want to select the last 20 lines of a text file and display them
in order starting with the last line first.....

Any assistance would be greatly appreciated....

Thanks, Chris

Jan 5 '07 #1
4 3590
Unless they've changed something in the last decade :-)

You're probably opening the textfile as a "stream of data" in which case you
must read forward. That leaves you with one of two solutions (in my mind.)
If the data isn't going to change as you are reading it you can make two
passes. The first one is to count how many lines you have, then you rewind
and skip as many lines as needed until you get to the point where 20 remain.
Append those to a collection and reverse the order just prior to using it.

The other method permits you to make a single pass but it means you will
store each line into a collection that contains a maximum of 20 items. Each
time you are about to store a new one you see if there are 20 and if so
delete the earliest one. At the end you will have a maximum of 20 items in
the collection. Reverse the order as above.

There is actually a third method. Simply read ever line into the
collection, reverse the order and limit the number of items you display.

Tom
"Stupid48" <cf*****@hotmai l.comwrote in message
news:11******** **************@ 38g2000cwa.goog legroups.com...
I'm trying to do a simple task but can't seem to find a solution. How
do I read lines from a text file backwards.

i.e. I want to select the last 20 lines of a text file and display them
in order starting with the last line first.....

Any assistance would be greatly appreciated....

Thanks, Chris

Jan 5 '07 #2
Thanks for the reply.....Yah, you'd think I could figure this out. I
don't know how to reverse the order of a collection. I guess that's
the problem. I'll keep searching.

Thanks again...

Tom Leylan wrote:
Unless they've changed something in the last decade :-)

You're probably opening the textfile as a "stream of data" in which case you
must read forward. That leaves you with one of two solutions (in my mind.)
If the data isn't going to change as you are reading it you can make two
passes. The first one is to count how many lines you have, then you rewind
and skip as many lines as needed until you get to the point where 20 remain.
Append those to a collection and reverse the order just prior to using it.

The other method permits you to make a single pass but it means you will
store each line into a collection that contains a maximum of 20 items. Each
time you are about to store a new one you see if there are 20 and if so
delete the earliest one. At the end you will have a maximum of 20 items in
the collection. Reverse the order as above.

There is actually a third method. Simply read ever line into the
collection, reverse the order and limit the number of items you display.

Tom
"Stupid48" <cf*****@hotmai l.comwrote in message
news:11******** **************@ 38g2000cwa.goog legroups.com...
I'm trying to do a simple task but can't seem to find a solution. How
do I read lines from a text file backwards.

i.e. I want to select the last 20 lines of a text file and display them
in order starting with the last line first.....

Any assistance would be greatly appreciated....

Thanks, Chris
Jan 5 '07 #3
Got it. Just created an arraylist and then looped through the index
backwards. Duh

Thanks again...

Stupid48 wrote:
Thanks for the reply.....Yah, you'd think I could figure this out. I
don't know how to reverse the order of a collection. I guess that's
the problem. I'll keep searching.

Thanks again...

Tom Leylan wrote:
Unless they've changed something in the last decade :-)

You're probably opening the textfile as a "stream of data" in which case you
must read forward. That leaves you with one of two solutions (in my mind.)
If the data isn't going to change as you are reading it you can make two
passes. The first one is to count how many lines you have, then you rewind
and skip as many lines as needed until you get to the point where 20 remain.
Append those to a collection and reverse the order just prior to using it.

The other method permits you to make a single pass but it means you will
store each line into a collection that contains a maximum of 20 items. Each
time you are about to store a new one you see if there are 20 and if so
delete the earliest one. At the end you will have a maximum of 20 items in
the collection. Reverse the order as above.

There is actually a third method. Simply read ever line into the
collection, reverse the order and limit the number of items you display.

Tom
"Stupid48" <cf*****@hotmai l.comwrote in message
news:11******** **************@ 38g2000cwa.goog legroups.com...
I'm trying to do a simple task but can't seem to find a solution. How
do I read lines from a text file backwards.
>
i.e. I want to select the last 20 lines of a text file and display them
in order starting with the last line first.....
>
Any assistance would be greatly appreciated....
>
Thanks, Chris
>
Jan 5 '07 #4
Stupid48 wrote:
i.e. I want to select the last 20 lines of a text file and display
them in order starting with the last line first.....
If you're using VS2005, you can use the System.IO.File. ReadAllLines()
function to read the file into a String array, with one array element for
each line in the file. Then just loop from the last string array element
backwards to display each of the lines of text.

--

(O)enone
Jan 5 '07 #5

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

Similar topics

7
17463
by: Jay | last post by:
I have a very large text file (being read by a CGI script on a web server), and I get memory errors when I try to read the whole file into a list of strings. The problem is, I want to read the file backwards, starting with the last line. Previously, I did: myfile = open('myfile.txt', 'r') mylines = myfile.readlines() myfile.close()
8
2351
by: nephish | last post by:
Hey there. i want to set a variable to represent the last line of a text file how do i do that? or even better, how do i create a list of the lines of a text file?
2
11801
by: Amy L. | last post by:
Is there a way through .net to read a very large text file (400MB+) backwards line by line. In system.io the filestream class has a "seek" method but the only read method requires you to know how many bytes to read in. My problem is that the line length of this log file is not constant so there is no easy way to read one line in. The only thing that is constant is that each line is terminated by a carriage return. If my only option...
15
3626
by: SK | last post by:
Hey folks, I am searching for a string (say "ABC") backwards in a file. First I seek to the end. Then I try to make a check like - do { file.clear (); file.get(c); file.seekg(-2, std::ios::cur);
18
5465
by: OrenFlekser | last post by:
Hi I've posted this message couple of days ago, but I can't find it now, so sorry if you see it twice... Anyways - I have a text box, and I want my users to be able to write only in english inside it. I want to prevent the Alt+Shift option of switching to other languages. Thanks alot - Oren
115
14186
by: TheAd | last post by:
At this moment I use MsAccess and i can build about every databound application i want. Who knows about a serious open source alternative? Because Windows will be a client platform for some time, i prefer a solution that (also) supports Windows. On the net I found a number of products that i looked at, but none of them gave me the impression of a serious candidate at this moment (KNoda, Gnome DB Manager, InterBase...). 2 additional...
14
7452
by: nic977 | last post by:
I am asked to write a simple program to displays the last n lines from a given text file. But I have no ideas how C defines a "line" in a text file. How does it tell if it is the end of the line, is there such thing call EOL like the EOF? -- Posted via http://dbforums.com
6
44119
by: Neil Patel | last post by:
I have a log file that puts the most recent record at the bottom of the file. Each line is delimited by a \r\n Does anyone know how to seek to the end of the file and start reading backwards?
5
4283
by: bbgun | last post by:
Hello all- I'm trying to write a progam that reads in a single text file, then copies the contents in reverse order to a new file. I'm guessing the most efficient way to do this is to read the file into a buffer (which I have done using fgets), then find where the file ends in this buffer, and read this backwards into the new file. Is that reasonable? What I don't understand is how to traverse this buffer and find the end of the file. If I...
0
9646
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10346
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
10096
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
9956
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
7504
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
6742
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
5386
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
4055
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
2
3658
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.