Qbasicnews.com

Full Version: A lighter "File not Found" routine
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Is there any way in FB/QB to test for the existence of a file, and have the program simply "move on" if it doesn't exist? That way the whole program doesn't crash from trying to interact with what doesn't exist.
My method is to use ON ERROR with OPEN...FOR INPUT -
[syntax="QBasic"]ON ERROR GOTO fileNotFound
OPEN "folder\fileName.ext" FOR INPUT AS #1: CLOSE #1
'{program goes here}
END 'END must be here unless you want to run your error handling section, even if there are no errors

fileNotFound:
IF ERR = 53 THEN PRINT "File Not Found. Ending program.": END[/syntax]

I hope that helps!
Quote:Is there any way in FB/QB to test for the existence of a file, and have the program simply "move on" if it doesn't exist? That way the whole program doesn't crash from trying to interact with what doesn't exist.
Simply "moving on" does not make any sense. What if the only input file to the program does not exist? Got no input, so "move on" to what?

The only thing you can do is detect the error, print an error message and terminate the program. That's not exactly "crashing" the program. Crashing it would happen if you don't detect the error, which would send an error number to the screen and abort the program.
*****
Quote:Simply "moving on" does not make any sense. What if the only input file to the program does not exist? Got no input, so "move on" to what?
Unlike you say, sometimes it is actually the best thing to do, to go on.
There are lots of examples to think of.
Unless I'm mistaken.
FB doesn't catch errors unless you compile it with that switch. When it detects and error it sets ERR and moves on.
Can't ya just do
Code:
OPEN "asdd23.txt" FOR INPUT AS #1
IF ERR THEN PRINT "File not found. Aborting":SLEEP:END
PRINT "Okay"
SLEEP
It probably has a specific error code but I dont know it =P
From genimplibs.bas (part of FB):

Code:
#ifndef FALSE
#define FALSE 0
#endif

#ifndef TRUE
#define TRUE (not FALSE)
#endif

'':::::
function hFileExists( byval filename as string ) as integer static
    dim f as integer

    if opt.overwrite then
        return FALSE
    end if

    f = freefile

    if( open( filename, for input, as #f ) = 0 ) then
        function = TRUE
        close #f
    else
        function = FALSE
    end if

end function

Regards,
Mark
Quote:
Moneo Wrote:Simply "moving on" does not make any sense. What if the only input file to the program does not exist? Got no input, so "move on" to what?
Unlike you say, sometimes it is actually the best thing to do, to go on.
There are lots of examples to think of.
Neo,

Please give some of these examples.

I'm taking about an input data file that the program needs to perform its processing. Not just a little input file that maybe contains the heading that goes on some output report, which obviously can still be generated without the heading.

I can't understand what you have in mind.
*****
... *looks up at overly complicated examples*

Code:
If DIR$(SomePath$+SomeFile$) <> "" Then Exist = 1
:roll:
If you had some generic stuff that would load in if a file doesn't load, or if the file is extra, then you don't stop everything and bail. 8)