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.

Newcomers AppGameKit Corner / [SOLVED] help with reading data from txt file

Author
Message
dandrews
3
Years of Service
User Offline
Joined: 26th Jun 2020
Location:
Posted: 18th Jul 2020 23:55 Edited at: 19th Jul 2020 01:27
im trying to learn how to read data from a txt file so eventually i can save data for my game as well. i can read the data in a string but im unsure how to separate that data ive tried the left and right commands but i can seem to get any results with printing anything but the entire string

the text file has this
50,1,0,1,1,1,0.40,0,0,8,0.20,0,0,0.3,0.8
i seen another example of using tokens as well that i tried with no luck

i also copied the example exactly and its not printing what it should

10
1.23
hello
it prints this

168636465
0.000001
0
hello

The author of this post has marked a post as an answer.

Go to answer

Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 19th Jul 2020 03:00 Edited at: 19th Jul 2020 03:11
from the ReadString() Help:
Quote: "Note that this can only read strings in binary form such as those written using WriteString, if you are trying to read strings separated by new lines then use ReadLine instead."

your data (50,1,0,1,1,1,0.40,0,0,8,0.20,0,0,0.3,0.8) is written in/saved as plain text (not binary), so ReadLine():


otherwise,
Quote: "i also copied the example exactly"

which example?
[My Itch.io Home]
[CODE lang=AGK] Your Code Here [/CODE]
[VIDEO=youtube] VideoID [/VIDEO]
dandrews
3
Years of Service
User Offline
Joined: 26th Jun 2020
Location:
Posted: 19th Jul 2020 03:12 Edited at: 19th Jul 2020 03:27
ok so if i were to add

it would read the next line in the txt file?
this is the example i was looking at https://www.appgamekit.com/documentation/examples/files/1_reading.htm
also with the code snippet you posted it prints out
1
1
Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 19th Jul 2020 03:26 Edited at: 19th Jul 2020 03:44
This post has been marked by the post author as the answer.
Quote: "it would read the next line in the txt file?"
yes, assuming there was one. if you know how many lines there are to read, that should be fine.

otherwise, if the lines were unknown (no idea what your project is), something like:
then manipulate/do what you want with those strings.

meanwhile, for the 2nd part of the first post:



bonus material: building and parsing strings can be handy. in my 2d billiards, the top ten scores, times and dates are all held in 1 string (as is the version number):
Build the String from a Typed array (Scores)


Save the String (HSString$, as cookie or otherwise):


Parsing the String (fill in the top 10 scores, after loading it):
[My Itch.io Home]
[CODE lang=AGK] Your Code Here [/CODE]
[VIDEO=youtube] VideoID [/VIDEO]
dandrews
3
Years of Service
User Offline
Joined: 26th Jun 2020
Location:
Posted: 19th Jul 2020 03:37
thank you i have some studying to do now
but if im understanding this correctly this should print the first five lines of the file?
Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 19th Jul 2020 03:51 Edited at: 19th Jul 2020 04:14
Quote: "this should print the first five lines "
yes (if there are 5 lines. i don't like the elseif you've added where you'll later have issues if you went to print the 4 additional strings that might not exist) but, "yes"

Quote: " i have some studying to do now"
when you've done that, note the bonus material i added to my last post for extra credit. there are tokens within tokens, there

add: here's a high score string if you want to play with the parsing (there's no prize so i don't care if people "hack" it ):
[My Itch.io Home]
[CODE lang=AGK] Your Code Here [/CODE]
[VIDEO=youtube] VideoID [/VIDEO]
dandrews
3
Years of Service
User Offline
Joined: 26th Jun 2020
Location:
Posted: 19th Jul 2020 03:56
ok i will thank you for the help as always
Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 19th Jul 2020 04:20 Edited at: 19th Jul 2020 04:22
Quote: "also with the code snippet you posted it prints out
1
1"

? i saved a plain text file named "thisdata.txt" in the media folder with 1 line:

and get:

with that code:

[My Itch.io Home]
[CODE lang=AGK] Your Code Here [/CODE]
[VIDEO=youtube] VideoID [/VIDEO]

Attachments

Login to view attachments
dandrews
3
Years of Service
User Offline
Joined: 26th Jun 2020
Location:
Posted: 19th Jul 2020 05:15
i just deleted the file and made another one with a different name and its working now im not sure what happened with the first txt file
Loktofeit
AGK Developer
15
Years of Service
User Offline
Joined: 21st Jan 2009
Location: Sarasota, FL
Posted: 29th Jul 2020 23:36

"this is the example i was looking at https://www.appgamekit.com/documentation/examples/files/1_reading.htm "

That example really should be edited to name the file "file.dat" as the ".txt" extension tends to be confusing for new users.



Login to post a reply

Server time is: 2024-04-25 10:20:37
Your offset time is: 2024-04-25 10:20:37