Sequential Files

INTRODUCTION:

It is true, that all great programs seem to have one thing in common, they all allow to save data in some form or another. Whether it's scores to a game, the whole game, data for a contact list, you name it, there are many reasons you can find to need the ability to save the data of your program to a file. This will be the purpose of this series of tutorials. To teach you how you can save your data to a file and retrieve it at a later time in your programs.

This tutorial assumes that you know your way around QuickBasic and it's statements. Since the examples shown here may use these statements, it would help if you knew them. Let's go right ahead.

WHAT ARE SEQUENTIAL FILES:

In this first installment, I will be teaching you how to work with Sequential files. What are sequential files? Well the best way to explain this is it is a file that is read from start to finish, character by character or line by line. There's more than one format of sequential file. Here are the more popular ones.

Comma Delimited File:

Comma delimited files are bits of information seperated by a comma. The number of commas in a given line read determines the number of fields that will need to be accomodated. In a typical scenario, a comma delimited file is read line by line and split into as many commas are found on the line for processing. This format is used alot for importing and exporting data between applications.

Fixed Length File:

Fixed length files are again read line by line only this time, the fields are aligned one under the other. Many programs, when they generate reports use this format. As far as reporting goes, the fixed length file is the most presentable form of sequential data.

Variable Length Files:

This is your regular, garden variety, text file. This tutor can most definitaly be categorized as a variable length file. It's a text file that has no specific formatting per se. It's usually used for editing purposes as a whole.

WORKING WITH SEQUENTIAL FILES:

Let's get right down to it shall we? In QuickBasic, as well as any Basic released after QBasic, all file manipulation statements are the same. What you'll learn in these 3 installments, will stay with you even in your Visual Basic projects that don't require so much data that a database is not needed.

Let's start by looking at a small sample program and it's related sequential file. Learning by example is often the best way to learn. I find that saying true especially when dealing with files. This example deals with a fixed length sequential file (because it's just clearer to read in an example program and data file). However, all coding done can be used for any kind of sequential data files that you can imagine.

' -------------------------------------

' VARIABLES REQUIRED FOR THIS PROGRAM

' -------------------------------------

DIM FileNumber AS LONG

DIM LineRead AS STRING

DIM ItemNumber AS LONG

DIM BookTitle AS STRING

DIM AuthorName AS STRING

DIM BookPrice AS DOUBLE

DIM TotalPrice AS DOUBLE

' -------------------------------------------------------------

' ACQUIRE NEXT FILE HANDLE AND OPEN THE DATA FILE (READ MODE)

' -------------------------------------------------------------

FileNumber = FreeFile

OPEN "TEST.TXT" FOR INPUT AS #FileNumber

' -------------------------------------------------

' LOOP THROUGH THE FILE UNTIL THE END OF THE FILE

' -------------------------------------------------

DO WHILE NOT EOF(FileNumber)

' ----------------------------------

' GET A LINE OF DATA FROM THE FILE

' ----------------------------------

LINE INPUT #FileNumber, LineRead

' ------------------------------------------------

' GET EACH BIT OF INFORMATION FROM THE LINE READ

' ------------------------------------------------

ItemNumber = VAL(LEFT$(LineRead, 4))

BookTitle = MID$(LineRead, 6, 35)

AuthorName = MID$(LineRead,36, 18)

BookPrice = VAL(MID$(LineRead, 60, 5))

' ---------------------------------

' DISPLAY THE ITEMS ON THE SCREEN

' ---------------------------------

PRINT ItemNumber; "="; BookTitle; " "; AuthorName; BookPrice

' -------------------

' SUM UP THE PRICES

' -------------------

TotalPrice = TotalPrice + BookPrice

LOOP

' -------------------------------------------

' DISPLAY THE TOTAL ACCUMULATED IN THE LOOP

' -------------------------------------------

PRINT

PRINT "PRICE TOTAL = ", TotalPrice

' -----------------------

' CLOSE THE FILE HANDLE

' -----------------------

CLOSE #FileNumber

The data file TEST.TXT should contain the following data to work properly with the example above.

------------------------START OF FILE---------------------------

0001 Compute's First Book of PC Unknown 3.99

0002 Compute's Second Book of PC Unknown 4.99

0003 Compute's third Book of PC Unknown 6.99

0004 Compute's Fourth Book of PC Unknown 10.99

-------------------------END OF FILE----------------------------

EXAMPLE NOTES:

Note that I used the FreeFile function to determine the next available file handle to be used in the OPEN statement. This is not a must. I could have used #1 or #n instead. However, In a bigger project that uses files alot, FreeFile assures that there is no conflicting File Handles used. It's good practice to use it whenever you're not sure of the number of files your program might need to open.

OPEN "TEST.TXT" FOR INPUT AS #FileNumber is the line that does the physical opening of the file And assigns it #FileNumber as it file handle. Here "TEST.TXT" is hardcoded as they say. Of course it could be a string variable and you could ask the user for the name of the file to open. The "FOR INPUT" part tells QuickBasic that you want to open the file in Read mode as you'll be reading the contents of the file later in the example.

Next is the loop indicated by the DO WHILE NOT EOF(FileNumber) line. In QuickBasic EOF() means End Of File. There is also BOF() for Beginning Of File and LOF() for Length Of File. Since you can't go backwards on a sequential file, you won't be needing BOF() at all. You'll see the use of BOF in the second installment for Random Access Files.

The first line Gets a line of Data from the data file Using this: LINE INPUT #FileNumber, LineRead which means, essentially, Get a line of text from #FileNumber and put it in LineRead, a string variable that I declared at the beginning of the example.

The next 4 lines in the loop are used to get the different bits and pieces of information from the line that we just read. Because we know, from the data file, the length of each piece of information on each line, we can do it the way I coded it. However some other types of sequential files won't allow you to work this way. For example, a Comma delimited file would usually require you to search for a comma (using the Instr() function) and getting the data between the location of the commas.

Next we print the information we gathered (for testing purposes) to the screen. This way you can make sure that the information was read properly.

Last line in the loop totals up the prices read. This is irrelevant to file manipulation but I just wanted to give this example program a purpose.

Right after the loop, we print a blank line and then print the total we just added up in the loop.

IMPORTANT: We then close the file handle. Many times This is forgotten by the programmer. But it's very important to never forget to close any and all file that you need to open. The stability of your data files will thank you.

Finally for this first installment, I'm always a firm believer that a file manipulation tutorial should show you both sides of the medal. Therefore, this next and final example will create the data file just as you saw it above in the read tutorial. After all, in order to read a data file, you have to be able to create it first, right? ;-). So here is the sample program to create the data file TEST.TXT.

' --------------------------------------------------------------

' ACQUIRE NEXT FILE HANDLE AND OPEN THE DATA FILE (WRITE MODE)

' --------------------------------------------------------------

FileNumber = FreeFile

OPEN "TEST.TXT" FOR OUTPUT AS #FileNumber

' ---------------------------------------------------

' WRITE THE LINES, ONE AFTER THE OTHER, IN THE FILE

' ---------------------------------------------------

PRINT #FileNumber, "0001 Compute's First Book of PC Unknown 3.99" + CHR$(13)

PRINT #FileNumber, "0002 Compute's Second Book of PC Unknown 4.99" + CHR$(13)

PRINT #FileNumber, "0003 Compute's third Book of PC Unknown 6.99" + CHR$(13)

PRINT #FileNumber, "0004 Compute's Fourth Book of PC Unknown 10.99" + CHR$(13)

' -----------------------

' CLOSE THE FILE HANDLE

' -----------------------

CLOSE #FileNumber

The data file TEST.TXT should now contain the following data.

0001 Compute's First Book of PC Unknown 3.99

0002 Compute's Second Book of PC Unknown 4.99

0003 Compute's third Book of PC Unknown 6.99

0004 Compute's Fourth Book of PC Unknown 10.99

Well yes, I used a quick and dirty trick to create the file, I just printed each line as you can see. Of course, you could have formatted the line from different variables to form the line of data if you wanted to.

NOTES:

Yes, again, I used the FreeFile function again. It's good practice, so I use it all the time. The important thing here is the the Open Statement itself. The main difference from the first example is the "FOR OUTPUT" part instead of the "FOR INPUT" part of the first example. This, of course, instructs QuickBasic to open the file in Write mode as you will be saving information to the file.

After opening the file, the 4 lines that follow perform the actual physical saving of data into the file with the PRINT #FileNumber, "Information" statement. Here I used string literals, but a String Variable could have been used as well but I believe that this small example makes things clear.

We then CLOSE the file handle. Like I said above, I can't stress enough the importance of not forgetting to do this. Like the old saying of "What goes up must come down!" I can say that "What gets opened must get closed too!".

And this is it for this first installment. I hope you learned something useful and be sure to look for my next installment where I will be covering Random access files.

Stephane Richard

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.