Read last 2 lines of a text file
by Tom Lenz · in Torque Game Builder · 10/06/2009 (2:15 pm) · 2 replies
I am looking for a way to read the last 2 lines of a text file.
The problem is that I do not know how many lines will be in each file. There could be 10 or 100.
Looking around I haven't found anything like getLineCount() or something similar.
I have come up with one way is to read all the lines and count how many I read while not recording any of the data. Then read the lines again, when getting to the last 2 then read them, but this seems very inefficient to have to read possibly 200 lines to get 2.
Any thoughts would be great.
The problem is that I do not know how many lines will be in each file. There could be 10 or 100.
Looking around I haven't found anything like getLineCount() or something similar.
I have come up with one way is to read all the lines and count how many I read while not recording any of the data. Then read the lines again, when getting to the last 2 then read them, but this seems very inefficient to have to read possibly 200 lines to get 2.
Any thoughts would be great.
#2
Sounds better than my idea,
but still seems like there should be something to get the count.
I'll use this until I find something better.
Thanks
10/06/2009 (2:30 pm)
Have two variables keep replacing each other, then when it gets to the end of the file, when there is nothing else to read you end up with the last two lines.Sounds better than my idea,
but still seems like there should be something to get the count.
I'll use this until I find something better.
Thanks
Associate William Lee Sims
Machine Code Games
%file = new file, open it for reading; %line0 = ""; %line1 = ""; while( NOT AT END OF FILE ) { %line0 = %line1; %line1 = %file.readLine(); }