Chapter 8 Sequential Files Chapter 8 Visual Basic
Chapter 8 Sequential Files Chapter 8 - Visual Basic Schneider 1
Outline and Objective • Creating Sequential Files • Adding items to Sequential Files • Using Sequential Files – Sorting Sequential Files – Merging Sequential Files – Control Break Processing Chapter 8 - Visual Basic Schneider 2
What is a File? • A collection of related data kept in secondary storage • Two types of files: program files, text(data) files • Data files are usually made up of a collection of records. – Holds Data about a single individual • A record is collection of fields. – Individual item Chapter 8 - Visual Basic Schneider 3
Creating a Sequential File • • • Choose a file name Choose a reference number Open the file for Output Write to the file Close the file Chapter 8 - Visual Basic Schneider 4
Example: Private Sub cmd. Create. File_Click() Dim name 1 As String, name 2 As String ' Demonstrate use of Write # statement Reference number Open "PIONEER. TXT" For Output As #1 Write #1, "ENIAC" File name Write #1, 1946 Write #1, "ENIAC", 1946 Write to the file name 1 = "Eckert" name 2 = "Mauchly" Write #1, 14 * 139, "J. P. " & name 1, name 2, "John" Close #1 Close the file End Sub Chapter 8 - Visual Basic Schneider 5
Adding Items to a Sequential File • • Choose a reference number for the file Open the file for Append Write to the end of the file Close the file Chapter 8 - Visual Basic Schneider 6
Example (Adding a player to a BASEBALL 1. TXT file) Private Sub Form_Load() Open App. Path & "BASEBALl. TXT" For Append As #1 End Sub Opening the file in append mode Private Sub cmd. Add. Rec_Click() Write #1, txt. Player. Text, txt. Times. Text, txt. Hits. Text txt. Player. Text = “ “ txt. Times. Text =“ ” txt. Hits. Text=“ ” txt. Player. Set. Focus End Sub Private Sub cmd. Quit_Click() Close #1 End Chapter 8 - Visual Basic End Sub Schneider 7
Sequential File • Different modes in which a file can be used: – Output – Input – Append • A file should not be open in two different modes at the same time. Chapter 8 - Visual Basic Schneider 8
Deleting or Changing a Record! • • Create a new file Read and change records Create a new one Erase the old one – Kill “File. Spec” • Rename the new file – Name “oldfilespec” As “newfilespec” Chapter 8 - Visual Basic Schneider 9
Error Trapping • Visual Basic has a device, called error-trapping, for preventing some types of errors. • If an error occurs while error-trapping is active, two things happen: 1. An identifying number is assigned to the Number property of an object called Err 2. The program jumps to error-handling routine. Chapter 8 - Visual Basic Schneider 10
Error Numbers Type of Error Value of Error Number Subscript out of range 9 Division by zero 11 File not found 53 File already open 55 File already exists 58 Disk full 61 Disk not ready 71 Chapter 8 - Visual Basic Schneider 11
Setting up error-trapping in a procedure: • Make the first line of the procedure: On Error Go. To Error. Handler • Type the statements to carry out the procedure • End the procedure by typing Exit Sub Error. Handler: error-handling routine Resume Chapter 8 - Visual Basic Schneider 12
Example ( error-handling routine to handle a “division by zero” error) Private Sub cmd. Divide_Click() On Error Go. To Error. Handler Dim a As Single, b As Single, c As Single pic. Result. Cls a = Val(Input. Box("Enter the numerator. ")) b = Val(Input. Box("Enter the denominator. ")) c=a/b pic. Result. Print ”The result is = “; c Exit Sub Chapter 8 - Visual Basic Schneider 13
The error-handling routine Error. Handler : Line Label ‘ Division by zero is error code 11 If Err = 11 Then pic. Result. Print "You tried to divide by 0, which is illegal" pic. Result. Print "Try again. ” b=Val(Input. Box(“Enter the denominator”)) End If Resume End Sub Chapter 8 - Visual Basic Schneider 14
Sorting Sequential Files • The records of a sequential file can be sorted on any field by first reading the data into parallel arrays and then sorting on a specific field, and then rewriting the data into a file. Chapter 8 - Visual Basic Schneider 15
Merging Sequential Files • Steps to merge two sorted files: 1. Open the two sorted files For Input and a third file For Output 2. Get the two items of data from each file 3. Compare and repeat until the EOF. If one item precedes the other, write it into the third file and get another item from the file If the two items are identical, write it into the third file and advance to the next items in both files. 4. Write the remaining items to the third file 5. Close three files. Chapter 8 - Visual Basic Schneider 16
- Slides: 16