how to read the last 20 lines of a text file?

Started by EnterTheStory (aka tolworthy), Fri 07/11/2008 10:32:03

Previous topic - Next topic

EnterTheStory (aka tolworthy)

I need to read the last 20 lines of a raw text file. It's very long., so I'd rather not read every single line, count them, then read again stopping at the desired line. Any suggestions?

Dualnames

Let me get this straight and right.. you want a 20 line txt file CONTENT to be used/placed in a String and used elsewhere(a GUI label ecc)?
Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

Lt. Smash

You could insert a section marker such as "->;>>;##" in your text file.
Then read in every line, safe its content into a String buffer;  and check if there is "->..." in it.
Then read in the next lines and safe its content into a String array or append them to a single String.

Snarky

Change the text file so that it's the first twenty lines you need to read.

Pumaman

I presume you're writing some sort of continuous log file, and want to have the game load in the last 20 lines when the game starts or something like that?

If that's the case probably the only thing you can do is go through the file reading all the lines one by one. However you don't then need to do it all again once you've counted them. Create an array of 20 Strings, and use them as a rotating buffer storing each line as you read it in. Then, when you hit the end of the file, your array will contain the last 20 lines.

EnterTheStory (aka tolworthy)

Quote from: Pumaman on Fri 07/11/2008 16:47:10
I presume you're writing some sort of continuous log file, and want to have the game load in the last 20 lines when the game starts or something like that?
Exactly! The log gets longer and longer as the game progresses.

Why not just write text in reverse order? Because you can read the log like a book, so it has to go from oldest to newest.

Why not just record text in the game separately? Because the user can save and load old logs, so the game must be dependent on the logs.

Why not read from the start? Because my game will be very large. Right now there are 3 or 4 thousand lines of dialog.  Every six months I'll add a similar number. Reading and processing a 20 thousand line file could make the game hang for a few seconds.

I was hoping there was some way to find the EOF (end of file) and just jump back a bit.

Trent R

So why not have 3 or 4 files with 1000 lines each?

~Trent
To give back to the AGS community, I can get you free, full versions of commercial software. Recently, Paint Shop Pro X, and eXPert PDF Pro 6. Please PM me for details.


Current Project: The Wanderer
On Hold: Hero of the Rune

EnterTheStory (aka tolworthy)

Quote from: Trent R on Fri 07/11/2008 20:15:34
So why not have 3 or 4 files with 1000 lines each?
~Trent
Complexity. The game will eventually contain at least 20 stories. Each will add maybe 3000 lines of text and may have 10-50 saved games.  Files can only be saved in the game folder, so it's going to become really crowded. The main purpose of the log is so users can find their own personal "story." If I break the files up they'll never find the one they want.

Pumaman

Well, it would be possible to add some extra File functions to AGS to allow you to seek to the end of the file and then start going backwards, but it's a bit of a specialized request. Perhaps a plugin would be the best answer here?

EnterTheStory (aka tolworthy)

Quote from: Pumaman on Fri 07/11/2008 21:16:39it's a bit of a specialized request.
I seem to specialize in those. :)

Then it sounds like my best bet is either to accept the tiny delay in reading a long file from the start, or design my game differently. Fair enough.

Trent R

Perhaps files can be read through only once at first, and then store which line is the EOF. Then once that is changed, flip a flag to tell that a new EOF needs to be found.

Just spouting crap here, don't know if it's possible.

~Trent
PS-Just noticed this for a Les Mis game, very interesting. I'll have to watch for this...
To give back to the AGS community, I can get you free, full versions of commercial software. Recently, Paint Shop Pro X, and eXPert PDF Pro 6. Please PM me for details.


Current Project: The Wanderer
On Hold: Hero of the Rune

monkey0506

#11
I always thought that seek methods for AGS file functions would be useful. Clearly file functions don't play a huge role in most adventure games, but I would say definitely if the seek function was available it would open up whole new worlds of possibility....just my two cents though.

Also a File.Size property would be great! :=

EnterTheStory (aka tolworthy)

Quote from: Trent R on Sat 08/11/2008 05:21:35
Perhaps files can be read through only once at first, and then store which line is the EOF.

Is there a way to jump to a particular line without reading everything before it? That would allow me to guess, and then just check if the EOF had been reached and guess again - five or six guesses would be enough for even the longest file.

RickJ

Do I understand correctly that you are trying to avoid locking out the player while the file is being read?   If so then have you considered reading the file in the background?  This could be done by reading some maximum number of lines each game cycle. 

I am guessing that the last 20 lines you are looking for is more or less the previous state of the game when the player exited and that you need to read these when then next session is started.   If so why couldn't you implement a cache of some sort.  There would be your master log file and there would be the cache file.  The master log would work the way it does now.  The cache would only contain the last 20 entries, or the entries from the last session or whatever yiou are looking for.   You would just have to write to two file when making a log entry.  On startup you would only need read the cache file  and then start a new one.

Pumaman

As a first step I'd just to a quick test to see if performance is an issue. Find a 5000 line text file and experiment with some script to read all the lines through to the end, and see how long it takes.

SMF spam blocked by CleanTalk