Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A lighter "File not Found" routine
#1
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.
Reply
#2
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!
974277320612072617420666C61696C21 (Hexadecimal for those who don't know)
Reply
#3
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.
*****
Reply
#4
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.
Reply
#5
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
[Image: sig.php]
Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.
Reply
#6
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
Reply
#7
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.
*****
Reply
#8
... *looks up at overly complicated examples*

Code:
If DIR$(SomePath$+SomeFile$) <> "" Then Exist = 1
:roll:
Reply
#9
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)
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)