Qbasicnews.com
A lighter "File not Found" routine - Printable Version

+- Qbasicnews.com (http://qbasicnews.com/newforum)
+-- Forum: Qbasic "like" compilers/interpreters (http://qbasicnews.com/newforum/forum-5.html)
+--- Forum: FB Discussion & Programming Help (http://qbasicnews.com/newforum/forum-15.html)
+--- Thread: A lighter "File not Found" routine (/thread-7938.html)



A lighter "File not Found" routine - Rokkuman - 08-22-2005

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.


A lighter "File not Found" routine - rpgfan3233 - 08-22-2005

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!


Re: A lighter "File not Found" routine - Moneo - 08-22-2005

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.
*****


A lighter "File not Found" routine - Neo - 08-22-2005

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.


A lighter "File not Found" routine - whitetiger0990 - 08-22-2005

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


A lighter "File not Found" routine - mjs - 08-23-2005

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


A lighter "File not Found" routine - Moneo - 08-23-2005

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.
*****


A lighter "File not Found" routine - Z!re - 08-23-2005

... *looks up at overly complicated examples*

Code:
If DIR$(SomePath$+SomeFile$) <> "" Then Exist = 1
:roll:


A lighter "File not Found" routine - Agamemnus - 08-23-2005

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)