Tweet |
Custom Search
|
|
STORING AND RETRIEVING DATA The programs we have written thus far dealt with data either stored in the program with DATA statements or input via an INPUT statement at time of program execution. These statements may be sufficient when handling data that is to be processed only once. However, there are times when you will want to save your data to be used by other programs. You are restricted in what you can do with the data when it is stored in DATA statements in a program. It is impractical to update data or to use the same data with different programs. It would be very time consuming to go through all the DATA statements and make changes, or to enter all new DATA statements each time you wanted to update and/or process the data. To eliminate having to enter the data each time it is to be processed, it can be stored independently on an auxiliary storage medium such as disk or tape. This keeps the data separate from the program so that it can be used with different programs without being rekeyed. Data stored in this manner is called a data file.A data file is any group of related records, such as inventory, personnel, payroll, manhour accounting, and so on. A record is composed of stocked. Each part and all the associated data about that part (part number, part name, and unit price) would make up a record. Part number, part name, and unit price are each a field. The following example shows a pictorial breakdown of a file composed of records and associated fields.Example: You might say, "All of this is fine, but how do I create and use these files?" Since the BASIC file processing statements are not standardized, you will have to refer to the users reference manual for your computer to find the file processing statements and their syntax. We will attempt only to deal with the file processing statements conceptually.Solving Problems With File Processing Statements Suppose you were keeping track of an inventory file for an auto parts store. You would need to create a file which would contain a record for each part in the inventory. This type of file is called a master file. Each record will contain the part number, part name, on-hand quantity, unit price, and reorder point (stock level). The flowchart in figure 7-1 describes one procedure that could be used to create the master file.Before you can write data records to a storage medium, you usually have to set up the file. Most computers do this with some form of open statement that names the file and makes it available for processing (block 1). Once the file is available for processing, the program will read the data elements from DATA statements just as in the past (block 2). Then the program checks for end-of-data (block 3); if there is data, it will write a record to the output file for each part in the inventory (block 4). Once all the data has been read and written you must tell the computer you are finished with that file. This is usually done with some form of close statement (block 5). At this point the computer does any necessary end-of-file processing then terminates the program (block 6).With a fundamental understanding of the problem and a flowchart as a guide, we are ready to write a program to create the master file: The program could be written as shown in figure 7-1.The DATA SAVE statement in line 10 opens the output file, making it available for the program to write data out to it, and assigns it the file name, INVFILE. The #2 tells the computer what storage medium is being used. LinesFigure 7-1.Flowchart and program to create a master file. 20 through 50 form a loop which reads data from DATA statements and writes records to the master file until all the data has been read and written. In line 20, the READ statement reads the data from the DATA statements, and line 30 tests the data for the "ENDDATA" record which indicates end-of-data. If it is not end-of-data, then the DATA SAVE statement in line 40 writes a record containing the values for the variable names listed. It writes the record onto the master file (INVFILE). The GOTO statement in line 50 transfers control back to line 20 to repeat the reading, testing, and writing of records until the READ statement reads the "ENDDATA" record and the IF-THEN test proves true. Then the program transfers control to line 60 which writes an end-of-file character on the master file and closes the file. The next executable statement is 999 which terminates execution of the program. For simplicity we have included only the data for one record in each DATA statement. This makes it easier to visualize what a record looks like. It also makes it easier to correct any data that is keyed incorrectly. Notice also that the DATA statements begin with much higher line numbers. This separates them from the program steps and allows room for expanding the program or for the addition of data without having to renumber the program. Using "ENDDATA" to indicate end-of-data rather than using a FOR-NEXT loop allows more flexibility in handling various amounts of data and eliminates having to know how much data will be processed. |
||