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 / Problems reading and writing from files

Author
Message
KareDev
12
Years of Service
User Offline
Joined: 6th Feb 2012
Location:
Posted: 24th Feb 2012 19:37
Earlier tonight, I wanted to add in a feature to my app that saves scores each time the user plays, so I tried to get some file reading/writing working.

Rather than put this into my app, instead I made a really simple program to do it - but it just won't seem to work.

I made a file called "savedata.txt", which contains the following text:

Quote: "SCORE_DATA_10_TOP_SCORES
110
100
90
80
70
60
50
40
30
20
10"


I then put this into my app's "media" folder, as well as the following folder:

C:\Documents and Settings\MY NAME\My Documents\AGK\C_ProgramFiles_TheGameCreators_AGKTrial_Projects_Basic_WriteReadFile_WriteReadFileexe\media

Here's the complete program:



I'm expecting, on startup for it to print:

"hello world"
1
SCORE_DATA_10_TOP_SCORES
A timer (so I can see if it's crashed)

However, it gives me an error saying the file isn't there. I can guarantee it is there. I'm staring at it right now.

So, then I decided to try WRITING a file. I changed around the statements to get the program to write a new file (one I hadn't yet created) called "test.txt", and just to write:

test test test test

over and over again, in that file. It did this fine; I have a 400kb file just saying "test test test" etc. over and over again. So then, I tried to read this file and display the strings onscreen each frame, like in the example above. Naturally I expected:

"hello world"
1
test
A timer

But instead I got this:

"hello world"
1
<blank line>
A timer

So now I'm really confused. I can't see what I'm doing wrong. Does AppGameKit have file reading disabled or something in the trial version?
KareDev
12
Years of Service
User Offline
Joined: 6th Feb 2012
Location:
Posted: 24th Feb 2012 19:54
OK, slight change to the above.

I was using Windows Notepad.

I tried instead opening savedata.txt in Notepad++, and simply saving it again, and that got rid of the "can not find file" error; the file was called "savedata.txt.txt".

However, the "reading" program is still failing to print anything from either of the files onto the screen. Can anyone tell me why?
KareDev
12
Years of Service
User Offline
Joined: 6th Feb 2012
Location:
Posted: 24th Feb 2012 20:02
:FACEPALM:

OK, figured it out. Despite my "test" file being really, really long, as the program is so simple, it's racing through the file and getting to EOF so fast that I missed it.
Paul Johnston
TGC Developer
22
Years of Service
User Offline
Joined: 16th Nov 2002
Location: United Kingdom
Posted: 27th Feb 2012 18:17
Also for a file like the one above



You will want to use ReadLine() to get each line one at a time. ReadString() reads a binary string written to a file with WriteString() only.
Marl
13
Years of Service
User Offline
Joined: 19th Nov 2011
Location: Bradford, UK
Posted: 1st Mar 2012 18:28 Edited at: 1st Mar 2012 18:36
You are reading the file in the main loop.

Each frame a new string will be read and so the entire file will be used up in about 11 Frames.

Now I don't know how fast you can read, but that's a bit fast for me

Remember that the screen is redrawn every loop, what was drawn the last loop is not kept but has to be redrawn each time.

Try this instead


I have tested this and it works with the data file in the media folder.

When your app writes a new file, this will be stored to the write area (my documents on a PC) and the new file will be read instead.

EDIT: Oops - I Missed your update a few posts up. but I'll leave the code up for anyone who wants to know the fix

Login to post a reply

Server time is: 2024-11-23 05:22:26
Your offset time is: 2024-11-23 05:22:26