[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Applesoft/Prodos/ONERR GOTO/File existence question



Eric Smith asked:

>Hans Franke and I submitted an entry to the Retrocode competition at
>VCF 6.0 today, and came in second.  I had a bit of a problem in one
>area, and thought maybe one of you experts out there might offer a
>suggestion.
>
>The code looked vaguely like this:
>
>10 DIM M%(38,38)
>20 GOSUB 10000: REM Load or generate maze
>
>...
>
>10000 GOSUB 20000: REM Try to load maze from data file
>10010 IF M%(0,0)=15 THEN RETURN: REM Loaded maze
>10020 REM Generate a new maze
>...
>10500 REM Write maze to file
>10510 PRINT CHR$(4);"OPEN MAZE.DAT"
>10520 PRINT CHR$(4);"WRITE MAZE.DAT"
>... nested loops with PRINT statements
>10580 PRINT CHR$(4);"CLOSE MAZE.DAT"
>10590 RETURN
>
>...
>
>20000 REM Try to read maze from file
>20010 ONERR GOTO 20040: REM In case the file doesn't exist
>20020 PRINT CHR$(4);"OPEN MAZE.DAT"
>20030 PRINT CHR$(4);"READ MAZE.DAT"
>... nested loops with INPUT statements
>20040 PRINT CHR$(4);"CLOSE MAZE.DAT"
>20050 POKE 216,0: REM Cancel ONERR
>20060 RETURN
>
>I'm not claiming that this is particularly good style.  We only had 3 hours
>to write the whole contest entry, and most of the time was spent on the game
>algorithms and debugging.
>
>Anyhow, the problem was that any time the program was run without a
>preexisting MAZE.DAT file, it would get the error and trap to line 20050,
>but when it got to the RETURN statement in 20060, it would then get a
>RETURN WITHOUT GOSUB error.
>
>So I have two questions:
>
>1)  Does an error trapped using ONERR really lose the subroutine stack?
>    Or do I need to do something else to make the RETURN work?  Note that
>    I definitely do NOT want to RESUME the statement that caused the
>    error.

It definitely leaves the stack messed up.  I remember there was a short
M/L program that could be POKEd into page 3 and called to clean things
up, but I always managed to work around the problem by getting my
recoverable errors outside of GOSUB nests. ;-)

>2)  Is there a better way from a BASIC program to determine the existence
>    of a file?

I would test for the existence of the file in the mainline part of the code,
prior to doing your GOSUBs.  The ONERR will work fine, and there will
be no need to reassemble Humpty Dumpty.  ;-)  You could just save
the existence/nonexistence of the file in a variable and leave your code
structure otherwise untouched.

If you're reading a variable-length file and counting on EOF to escape
the loop, then you're probably stuck with recovering properly from
the ONERR exit.  I'm sure someone here remembers the code--it was
just a few instructions.

-michael

Check out amazing quality sound for 8-bit Apples on my
Home page:  http://members.aol.com/MJMahon/