Computer Science Canada

Help with eof

Author:  chipanpriest [ Wed Jan 11, 2012 5:59 pm ]
Post subject:  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:

Turing:

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:

Turing:

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:

Turing:

    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

Author:  chipanpriest [ Wed Jan 11, 2012 6:09 pm ]
Post subject:  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??

Author:  Tony [ Wed Jan 11, 2012 6:13 pm ]
Post subject:  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
code:

...
put "Writing highscore"
open : f, "highscore.txt", put, mod, seek
...

Maybe your if-conditions are not true?

Also, it seems that the file will hold at most one value. In which case you are not using mod, seek options.

Author:  chipanpriest [ Wed Jan 11, 2012 6:17 pm ]
Post subject:  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.

Turing:

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

Author:  Dreadnought [ Wed Jan 11, 2012 6:32 pm ]
Post subject:  Re: Help with eof

This
Turing:
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 File module, it has just what you need.

Author:  chipanpriest [ Wed Jan 11, 2012 9:44 pm ]
Post subject:  RE:Help with eof

ooohhhhh thanks so much Very Happy i didnt know that erased it haha xD

Author:  chipanpriest [ Wed Jan 11, 2012 9:49 pm ]
Post subject:  Re: Help with eof

Yea! It works now guys! Thanks dreadnaught Very Happy all i did was change this:
Turing:
open : f, "highscore.txt", put
close : f


to this:
Turing:
if File.Exists ("highscore.txt") = false then
    open : f, "highscore.txt", put
    close : f
end if

Author:  Alex C. [ Wed Jan 11, 2012 10:00 pm ]
Post subject:  RE:Help with eof

using the same idea, is it possible to create a save file for a game?

Author:  Tony [ Wed Jan 11, 2012 11:27 pm ]
Post subject:  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.

Author:  chipanpriest [ Thu Jan 12, 2012 7:03 am ]
Post subject:  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?


: