Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / Reading lines from a text file [Tier 1]

Author
Message
Funnell7
12
Years of Service
User Offline
Joined: 8th Sep 2011
Location: UK, England
Posted: 3rd Aug 2012 17:25 Edited at: 3rd Aug 2012 17:28
Hello, I'm trying to read seperate lines from a text file but am not having much joy... For example, my text file simply contains the following:



Now when I try and read this and store it as a string, I am doing this:



'Print(String)' then prints:



As I would expect... However I'm now trying to read the '4' using the Mid command, but it doesn't display anything, i.e. Print(Mid(String, 4, 1))

I'm clearly going the wrong way about this, is there a way of specifying which line to read from the text file so I can store each line in a seperate string? Or is that just mental?

Thank you, as always...

EDIT: Dodgy code brackets...
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 3rd Aug 2012 18:12
Could be it's detecting the return character chr(10) probably. Check the length of the string.


this.mess = abs(sin(times#))
Funnell7
12
Years of Service
User Offline
Joined: 8th Sep 2011
Location: UK, England
Posted: 3rd Aug 2012 18:24
Genius... You're right, the string length is 8. So I need to account for 2 extra characters when moving to the next line. Thanks Baxslash...

Just out of interest, am I going the right way about this? Say I have a text file which stores the option settings, i.e.



I was going to have some form of parsing function to bring the settings back in. In your opinion, is there a better way to do this?

Many thanks!
DVader
20
Years of Service
User Offline
Joined: 28th Jan 2004
Location:
Posted: 3rd Aug 2012 18:31 Edited at: 3rd Aug 2012 18:36
Baxslash beat me to it. Here's one of the ways I do this sort of thing.

This grabs the in code data and loads it into an array. You can save that back out as a text file to be read in later, rather than using the in code data. I do this for all my games, thus having a default highscore. Once There is a file made by AppGameKit it refers to that instead of the data.

That is the code I use to save the array data. It is then just a matter of changing open to write to the read command to load it back in. I have not included all the code that sorts each part of the array, just the names part. The other data at the start, and the reason for the for x loop in the last part are for score etc.

Funnell7
12
Years of Service
User Offline
Joined: 8th Sep 2011
Location: UK, England
Posted: 3rd Aug 2012 18:46
Thanks DVader! I am doing something very similar for my online leaderboard, the difference was that my online leaderboard is returned as one long string so is easily parsed. I just wasn't sure how to account for new lines, but it looks as though I can just continue with the 'Mid' command until I find what I'm after! Thank you for the reply and the examples!!
DVader
20
Years of Service
User Offline
Joined: 28th Jan 2004
Location:
Posted: 3rd Aug 2012 19:06
Yes, the mid command will read through the entire string, it treats each new line as a carriage return as baxslash said. The comma separated data at the start was the easiest way to have default data, that does not rely on any file to be present. If the highscore file is deleted by mistake for instance the game will just generate the default one instead and re save it.

Van B
Moderator
21
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 3rd Aug 2012 19:17
One issue that might have an effect, is that your reading a string, so it might not expect the data to have standard formatting - plus I'm sure ReadString will load the whole shebang into that variable.

If you need to read a single, CHR(10) delimited line, then use READLINE() instead... like read each line, and extract it using mid().

Health, Ammo, and bacon and eggs!
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 3rd Aug 2012 20:29
Based on the file you listed, which has carriage returns, you want to use 'ReadLine', not 'ReadString'.

ReadString will read anything it sees until it reaches a null character indicating the end of string.

If you are using a text file that is human readable and editable, then you really want to use ReadLine.

The ReadString, ReadFloat and ReadInt commands are for working with binary files, not human readable text files.

Cheers,
Ancient Lady
AGK Community Tester

Login to post a reply

Server time is: 2024-04-27 19:36:22
Your offset time is: 2024-04-27 19:36:22