
-----------------------------------
chipanpriest
Wed Jan 11, 2012 5:59 pm

Help with eof
-----------------------------------
Hey guys. I'm making a game that stores a high score onto a text file and I need some help. Now, I do have basic understanding of open, close, put, get, seek so I'm not entirely lost. The code I am using is similar to this:


var f : int
var high : int %highscore variable

%makes the text file
open : f, "highscore.txt", put
close : f
%/makes the text file

open : f, "highscore.txt", get
get : f, high
close : f


The error says it is attempting to read past the end of file (eof) and I understand what the problem is. There is nothing in the text file so it cant get anything out of it. What I need help with is if somebody could tell be how I could get the program to ignore the get statement if there is nothing in the text file. Any help will be greatly appreciated!  :canada:

I aim to have something like this:


if eof (f) then
    high := 1
else
    get : f, high
end if




EDIT
I fixed the problem I was having but I ran into another one. Now when I exit the program, it doesn't save the highscore to my text file even though I told it to. This is the code I'm using:


    if lose = true then
        if lvl > high then
            open : f, "highscore.txt", put, mod, seek
            put : f, lvl
            close : f
        end if
        lvl := 1
    end if


-----------------------------------
chipanpriest
Wed Jan 11, 2012 6:09 pm

Re: Help with eof
-----------------------------------
When I exit the program and start it back up, my highscore is reset and I was curious as to why so I opened up my text file and there was nothing in it! Yet you even saw the code I posted. Should it not put my highscore into the text file??

-----------------------------------
Tony
Wed Jan 11, 2012 6:13 pm

RE:Help with eof
-----------------------------------
sorry, my post was in reply to before your edit, so I deleted it.

Do you know that your code even tries to write anything? You can check for what with
mod, seek options.

-----------------------------------
chipanpriest
Wed Jan 11, 2012 6:17 pm

Re: Help with eof
-----------------------------------
Here, maybe you aren't getting enough code to judge if there is a problem or not. Ill post more code and I'll just attach the file.


open : f, "highscore.txt", put
close : f
%------------------------------
open : f, "highscore.txt", get
if eof (f) then
    high := 1
else
    get : f, high
end if
close : f
%------------------------------
if lose = true then
    if lvl >= high then
        open : f, "highscore.txt", put, mod, seek
        put : f, lvl
        close : f
    end if
    lvl := 1
end if


-----------------------------------
Dreadnought
Wed Jan 11, 2012 6:32 pm

Re: Help with eof
-----------------------------------
This open : f, "highscore.txt", put 
close : f Erases the contents of the text file.

You said you wanted to make sure a highscores file exists (edit: I felt that was what you wanted to do). Read up on the [url=http://compsci.ca/holtsoft/doc/filemodule.html]File module, it has just what you need.

-----------------------------------
chipanpriest
Wed Jan 11, 2012 9:44 pm

RE:Help with eof
-----------------------------------
ooohhhhh thanks so much :D i didnt know that erased it haha xD

-----------------------------------
chipanpriest
Wed Jan 11, 2012 9:49 pm

Re: Help with eof
-----------------------------------
Yea! It works now guys! Thanks dreadnaught :D all i did was change this:
open : f, "highscore.txt", put
close : f


to this:
if File.Exists ("highscore.txt") = false then
    open : f, "highscore.txt", put
    close : f
end if


-----------------------------------
Alex C.
Wed Jan 11, 2012 10:00 pm

RE:Help with eof
-----------------------------------
using the same idea, is it possible to create a save file for a game?

-----------------------------------
Tony
Wed Jan 11, 2012 11:27 pm

RE:Help with eof
-----------------------------------
Yes. The only difference is that instead of a single value, you'd write enough to reconstruct the state of the game.

-----------------------------------
chipanpriest
Thu Jan 12, 2012 7:03 am

Re: Help with eof
-----------------------------------
Does turing allow you to encrypt saved files into the program file? Or am I going to need a separate text file for every game i decide to have a save for?
