QBasic/Files

In this lesson, we will learn how to create and modify files. In doing so, we will create a portion of a text editor to handle reading and writing files to and from disk - the program won't be complete by the end of this chapter, but will be finished within Advanced Text Output.

Let's start by setting up out main procedure:

'$DYNAMICON ERRORGOTOhandler' Prepares the error handlerDIMtext(50)ASSTRING' Used to contain the text file.maxlines=50' Contains the current size of the buffer.DOCLS'clears the screenINPUT"Would you like to create a (N)ew file, (L)oad an existing one, or (E)xit the program";choice$SELECT CASEUCASE$(choice$)'UCASE$ converts strings to UPPER CASECASE"N"'New fileCASE"L"'Load a fileCASE"E"'ExitCLSENDENDSELECTLOOP'returns to the top of the program.handler:errorflag=ERR' Keep track of the error that occurred.RESUMENEXT' Proceeds with the next statement.

As you can see, we are using CASE rather than IF. IF statements can sometimes work better than case statements, but for now, we want to avoid spaghetti code (where there are too many GOTO's).

So far, we don't really have much, but it's a start. We've asked the user what they want to do, and finished 1/3 options. Not so shabby when you put it that way!

Contents

The open statement allows either reading or writing information from the disk. In general, the open statement follows this pattern:

OPENfile$FORINPUTAS1OPENfile$FOROUTPUTAS2

The file$ determines the filename to use. The FOR portion indicates how the file will be accessed or operated - it may be APPEND, BINARY, INPUT, OUTPUT, and RANDOM. The AS # is the identifier used for the file handle in question - this may be a variable if desired.

Note:
If you allow the user to enter a filename that does not exist on disk, you need to implement error handling using ON ERROR to react to this situation.

We will now add a subroutine to read the complete file from disk, as lines of text, into an string array called text(). It is also possible to read a data file full of numerical values (and input these into a number array), however that is a different topic.

Note the code that finds the file 'size', by reading lines one at a time until the End Of File is reached, and the use of 'SEEK' to 'rewind' to the beginning again.

SUBLoadFileSHAREDfilename$SHAREDlines,maxlinesSHAREDtext()ASSTRINGSHAREDerrorflagINPUT"Enter filename: ";filename$OPENfilename$FORINPUTAS1IFerrorflag<>0THENerrorflag=0CLOSEPRINT"File not found - press return to continue."INPUT"",a$EXITSUBENDIF' Count the number of lines.lines=0DOWHILENOTEOF(1)LINEINPUT#1,l$lines=lines+1LOOP'Allocate enough space for input. IFmaxlines>linesTHENREDIMtext(lines+25)ASSTRINGmaxlines=lines+25ENDIFSEEK#1,1' Rewind to the beginning of the file.' Read the lines into the bufferFORcline=1TOlinesLINEINPUT#1,text(cline)NEXTCLOSE1errorflag=0ENDSUB

The example above treats the file as type=text. If the file contains numbers (for example, a data array of N integers per line x M lines) these can be read (input #) one at a time, directly into a numeric array. Input will read the numbers one at a time, 'stopping' after each is input. Numbers can be separated by 'anything' (so lines of text will be skipped).