Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: EnterTheStory (aka tolworthy) on Fri 07/11/2008 10:32:03

Title: how to read the last 20 lines of a text file?
Post by: EnterTheStory (aka tolworthy) on Fri 07/11/2008 10:32:03
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?
Title: Re: how to read the last 20 lines of a text file?
Post by: Dualnames on Fri 07/11/2008 12:47:04
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)?
Title: Re: how to read the last 20 lines of a text file?
Post by: Lt. Smash on Fri 07/11/2008 13:06:40
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.
Title: Re: how to read the last 20 lines of a text file?
Post by: Snarky on Fri 07/11/2008 14:38:45
Change the text file so that it's the first twenty lines you need to read.
Title: Re: how to read the last 20 lines of a text file?
Post by: 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?

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.
Title: Re: how to read the last 20 lines of a text file?
Post by: EnterTheStory (aka tolworthy) on Fri 07/11/2008 20:08:49
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.
Title: Re: how to read the last 20 lines of a text file?
Post by: Trent R on Fri 07/11/2008 20:15:34
So why not have 3 or 4 files with 1000 lines each?

~Trent
Title: Re: how to read the last 20 lines of a text file?
Post by: EnterTheStory (aka tolworthy) on Fri 07/11/2008 20:43:19
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.
Title: Re: how to read the last 20 lines of a text file?
Post by: Pumaman on Fri 07/11/2008 21:16:39
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?
Title: Re: how to read the last 20 lines of a text file?
Post by: EnterTheStory (aka tolworthy) on Sat 08/11/2008 00:46:20
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.
Title: Re: how to read the last 20 lines of a text file?
Post by: 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. 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...
Title: Re: how to read the last 20 lines of a text file?
Post by: monkey0506 on Sat 08/11/2008 08:41:10
I always thought that seek (http://msdn.microsoft.com/en-us/library/system.io.filestream.seek(VS.71).aspx) 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! :=
Title: Re: how to read the last 20 lines of a text file?
Post by: EnterTheStory (aka tolworthy) on Sat 08/11/2008 10:38:37
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.
Title: Re: how to read the last 20 lines of a text file?
Post by: RickJ on Sun 09/11/2008 01:47:59
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.
Title: Re: how to read the last 20 lines of a text file?
Post by: Pumaman on Sun 09/11/2008 01:49:43
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.