GLBasic forum

Main forum => GLBasic - en => Topic started by: S. P. Gardebiter on 2008-Mar-26

Title: Program hangs up because of DOESFILEEXIST()
Post by: S. P. Gardebiter on 2008-Mar-26
If I execute the following code, my program hangs up for whatever reason:

Code (glbasic) Select
LET Config
LET ConfigSaved
LET file$

Config = DOESFILEEXIST("Config.dat")
IF Config
ConfigSaved = TRUE
OPENFILE(1, "Config.dat", TRUE)
READLINE 1, file$
CLOSEFILE 1
GOTO Open
ELSE
ConfigSaved = FALSE
PRINT "Please type exectuable name:", 16, 16
INPUT file$, 16, 32
GOTO Open
ENDIF
Title: Program hangs up because of DOESFILEEXIST()
Post by: PeeJay on 2008-Mar-26
Aaaargh, the curse of GOTO - and what is LET????

Try this:-

Code (glbasic) Select
LOCAL Config,ConfigSaved,file$

Config = DOESFILEEXIST("Config.dat")
IF Config=TRUE
    ConfigSaved = TRUE
    OPENFILE(1, "Config.dat", TRUE)
    READLINE 1, file$
    CLOSEFILE 1
ELSE
    ConfigSaved = FALSE
    PRINT "Please type exectuable name:", 16, 16
    INPUT file$, 16, 32
ENDIF
OpenFunc(ConfigSaved,file$)

FUNCTION OpenFunc: ConfigSaved,file$
.
.
.
ENDFUNCTION
Title: Program hangs up because of DOESFILEEXIST()
Post by: S. P. Gardebiter on 2008-Mar-26
Ahh yea, much better. Thanks.
So it doesn't work with GOTO?
Title: Program hangs up because of DOESFILEEXIST()
Post by: PeeJay on 2008-Mar-26
GOTO works, but it is horrible! The problem with GOTO is that it breaks up program logic, and can be too easy to get into an endless loop. With this in mind, it is considered very bad practice to use GOTO in code these days.

Happy to help :)

Edit: As you can see by my code edit, it is easy to pass local variables to a function, and is preferable to having loads of GLOBAL variables - again, it makes for neater, self contained code - once you have written your "OpenFunc:" function, you could just copy and paste it into your next project with no need to edit anything ;)
Title: Program hangs up because of DOESFILEEXIST()
Post by: bigsofty on 2008-Mar-26
Spots a "GOTO"... runs away!,,, :giveup:
Title: Program hangs up because of DOESFILEEXIST()
Post by: Ian Price on 2008-Mar-26
I haven't used a GOTO since coding on the CPC, 20 years ago! It has its uses, but to me it's pure evil in code form. :p
Title: Program hangs up because of DOESFILEEXIST()
Post by: Kitty Hello on 2008-Mar-27
If I have a double nested loop (for x... for y... ) and want to exit from inside,  use goto. Only there.
Title: Program hangs up because of DOESFILEEXIST()
Post by: Moru on 2008-Mar-27
*shudder* that would certainly cause troubles with some basic dialects. If you jump out of the nested for...next It would spend the rest of the program searching for the missing nexts :-)

Only time I ever used GOTO was in the same sentence as ON ERROR :-)

Oh, and in assembler when doing just about anything... BEQ or whatever :-)