Click here to LOGIN
Information
Tutorials & Articles
Programs
Feedback
 


Forum
Add a Post
Username:   (You must log on to use your member username)Hint: Use [code] and [/code] around text to highlight it as QB code.


Subject:
Message:
Forums -> Q & A -> Error handling when opening files
sintral
Error handling when opening files
Posted Aug 26 2009
I have a short program that attempts to read in a set of files (using READ/DATA statements in a loop). In some cases, one or more of the files may not exist and will need to be skipped. What is the preferred method of handling the errors generated by attempting to open non-existing files that doesn't involve GOTO's and line numbers? Here is the code:


CLS
DO WHILE (LEN(r$) <> 6)
INPUT "Enter Date to Summarize YYMMDD (Enter only to Quit) ", r$
IF r$ = "" THEN END
LOOP

f$ = "20" + r$ + ".TXT"
OPEN f$ FOR OUTPUT AS #1
DATA WW,FI,CU,SE,IS,OS,PC,PU,SH

FOR x = 1 TO 9
READ pref$
ff$ = pref$ + r$ + ".TXT"
OPEN ff$ FOR INPUT AS #2
DO WHILE NOT EOF(2)
LINE INPUT #2, rr$
PRINT #1, rr$
z = z + 1
LOCATE 3, 5: PRINT z
LOOP
CLOSE #2
NEXT x
CLOSE : END



The portion in question is:

OPEN ff$ FOR INPUT AS #2

Clippy
OPEN the file for APPEND first
Posted Aug 26 2009
Open for INPUT will create an error if a file does not exist or is read when empty.

So you create the file if it did not exist with APPEND. Then you get the LOF(2). If the LOF = 0 then the file is empty. So why read it? In fact you can KILL it also after you CLOSE it.



The QBasic Station, (C) Copyright 1997-2010