Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 8 - Visual Basic Schneider1 Chapter 8 Sequential Files.

Similar presentations


Presentation on theme: "Chapter 8 - Visual Basic Schneider1 Chapter 8 Sequential Files."— Presentation transcript:

1

2 Chapter 8 - Visual Basic Schneider1 Chapter 8 Sequential Files

3 Chapter 8 - Visual Basic Schneider2 Outline and Objective Creating Sequential Files Adding items to Sequential Files Using Sequential Files –Sorting Sequential Files –Merging Sequential Files –Control Break Processing

4 Chapter 8 - Visual Basic Schneider3 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. A record is collection of fields.

5 Chapter 8 - Visual Basic Schneider4 Creating a Sequential File Choose a file name Choose a reference number Open the file for Output Write to the file Close the file

6 Chapter 8 - Visual Basic Schneider5 Example: Private Sub cmdCreateFile_Click() Dim name1 As String, name2 As String ' Demonstrate use of Write # statement Open "PIONEER.TXT" For Output As #1 Write #1, "ENIAC" Write #1, 1946 Write #1, "ENIAC", 1946 name1 = "Eckert" name2 = "Mauchly" Write #1, 14 * 139, "J.P. " & name1, name2, "John" Close #1 End Sub File name Reference number Write to the file Close the file

7 Chapter 8 - Visual Basic Schneider6 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

8 Chapter 8 - Visual Basic Schneider7 Example (Adding a player to a BASEBALL1.TXT file) Private Sub Form_Load() Open App.Path & "\BASEBALl.TXT" For Append As #1 End Sub Private Sub cmdAddRec_Click() Write #1, txtPlayer.Text, txtTimes.Text, txtHits.Text txtPlayer.Text = “ “ txtTimes.Text =“ ” txtHits.Text=“ ” txtPlayer.SetFocus End Sub Private Sub cmdQuit_Click() Close #1 End End Sub Opening the file in append mode

9 Chapter 8 - Visual Basic Schneider8 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.

10 Chapter 8 - Visual Basic Schneider9 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.

11 Chapter 8 - Visual Basic Schneider10 Setting up error-trapping in a procedure : Make the first line of the procedure: On Error GoTo ErrorHandler Type the statements to carry out the procedure End the procedure by typing Exit Sub ErrorHandler: error-handling routine Resume

12 Chapter 8 - Visual Basic Schneider11 Example ( error-handling routine to handle a “division by zero” error) Private Sub cmdDivide_Click() On Error GoTo ErrorHandler Dim a As Single, b As Single, c As Single picResult.Cls a = Val(InputBox("Enter the numerator.")) b = Val(InputBox("Enter the denominator.")) c = a / b picResult.Print ”The result is = “; c Exit Sub

13 Chapter 8 - Visual Basic Schneider12 The error-handling routine Exit Sub: ‘ Division by zero is error code 11 If Err = 11 Then picResult.Print "You tried to divide by 0, which is illegal" picResult.Print "Try again.” b=Val(InputBox(“Enter the denominator”)) End If Resume End Sub Line Label

14 Chapter 8 - Visual Basic Schneider13 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.

15 Chapter 8 - Visual Basic Schneider14 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 the three files.

16 Chapter 8 - Visual Basic Schneider15 Control Break Processing While processing sequential files, you can introduce control variables to trigger a computation or a specific task. Each change in value of the control variable is called a break.

17 Chapter 8 - Visual Basic Schneider16 Common Errors Opening the file in wrong mode Not using Write statement every time you need to add data to a file Not closing the file


Download ppt "Chapter 8 - Visual Basic Schneider1 Chapter 8 Sequential Files."

Similar presentations


Ads by Google