Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 8 - Visual Basic Schneider

Similar presentations


Presentation on theme: "Chapter 8 - Visual Basic Schneider"— Presentation transcript:

1 Chapter 8 - Visual Basic Schneider
Sequential Files Chapter 8 - Visual Basic Schneider

2 Chapter 8 - Visual Basic Schneider
Outline and Objective Creating Sequential Files Adding items to Sequential Files Using Sequential Files Sorting Sequential Files Merging Sequential Files Chapter 8 - Visual Basic Schneider

3 Chapter 8 - Visual Basic Schneider
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. Name First second Ali Access method: the way in which the computer transmits data between secondary storage and the primary storage. Huda record Ahmed field Chapter 8 - Visual Basic Schneider

4 Creating a Sequential File
Choose a file name Choose a reference number Open the file for Output Write to the file Close the file Open “FileName.txt” for Output as #1 Write#1, “Hello”, 5 Close #1 This means that if you want to access a particular data item, all the data proceeds the needed item must be accessed first. Chapter 8 - Visual Basic Schneider

5 Chapter 8 - Visual Basic Schneider
Example: Private Sub cmdCreateFile_Click() Dim name1 As String, x as Integer ' Demonstrate use of Write # statement Open “C:\user\temp\Marks.txt" For Output As #1 Write #1, “Ali" Write #1, 22 Write #1, “Ahmed", 23 name1 = “Huda“ x= 20 Write #1, name1, x+2 Close #1 End Sub Reference number File name Write to the file Close the file Chapter 8 - Visual Basic Schneider

6 Chapter 8 - Visual Basic Schneider
Example: Write the string CS surrounded by quotation marks into the file Write the number 116 without any leading or trailing space into the file Private Sub cmdCreateFile_Click() Dim name1 As String, name2 As String Open “C:\user\Data.txt" For Output As #1 Write #1, “CS" Write #1, 116 Write #1, “BASIC", 10, 20, ”One ” name1 = “Student“ name2 = “Test" Write #1, 2*3, “ID “ & name1, name2 Close #1 End Sub “CS” 116 “BASIC",10,20,”One ” 6,“ID Student”,“Test” 6 Chapter 8 - Visual Basic Schneider

7 Chapter 8 - Visual Basic Schneider
Example: Chapter 8 - Visual Basic Schneider

8 Chapter 8 - Visual Basic Schneider
Example: Chapter 8 - Visual Basic Schneider

9 Chapter 8 - Visual Basic Schneider
Example: Chapter 8 - Visual Basic Schneider

10 Chapter 8 - Visual Basic Schneider
Caution If an existing file is opened for output, the computer will erase the existing data and create a new empty file. Chapter 8 - Visual Basic Schneider

11 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 1. Reference number should be from 1 through 511. It need not be the number that was used when the file was created. 2. It allows data to be output and recorded at the end of the specified file. 3. Write with the # write statements 4. Close with the same reference number Note: Append is intended to add data to an existing file, but also can be used to create a new file. Chapter 8 - Visual Basic Schneider

12 Chapter 8 - Visual Basic Schneider
Assume that the file “data.txt” contains the following entries:10,20,30,40 What is the content of the file “data.txt” after the user click on the command button Chapter 8 - Visual Basic Schneider

13 Chapter 8 - Visual Basic Schneider
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. 1. The name of the file: follows the same rules for naming program files. Use invest.bas and invest.dat 2. The mode in which the file is to be used , can be output, input, append . Output: indicates data is written to the file from the program input: the contents of the file are being read into the program append :allows new records to be added to an existing file. A butter is a reserved part of the primary storage unit used a s temporary storage area for the data that is being written or read from a file Chapter 8 - Visual Basic Schneider

14 Chapter 8 - Visual Basic Schneider
ERROR: A file should not be open in two different modes at the same time. Chapter 8 - Visual Basic Schneider

15 Chapter 8 - Visual Basic Schneider

16 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

17 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. Chapter 8 - Visual Basic Schneider

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

19 Deleting or Changing a Record!
Create a new file Read and change records Create a new one Erase the old one Kill “FileSpec” Rename the new file Name “oldfilespec” As “newfilespec” 19 Chapter 8 - Visual Basic Schneider

20 Chapter 8 - Visual Basic Schneider
Example: before After Chapter 8 - Visual Basic Schneider

21 Chapter 8 - Visual Basic Schneider
Example: After before Chapter 8 - Visual Basic Schneider

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

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

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

25 The error-handling routine
errorHandler: ‘ 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 Chapter 8 - Visual Basic Schneider

26 Chapter 8 - Visual Basic Schneider
Example: Chapter 8 - Visual Basic Schneider

27 Example ( error-handling routine to handle a “division by zero” error)
27 Chapter 8 - Visual Basic Schneider

28 Chapter 8 - Visual Basic Schneider
Example: 28 Chapter 8 - Visual Basic Schneider

29 Chapter 8 - Visual Basic Schneider
Example: Chapter 8 - Visual Basic Schneider

30 Chapter 8 - Visual Basic Schneider
Example: Chapter 8 - Visual Basic Schneider

31 Chapter 8 - Visual Basic Schneider
Example: Chapter 8 - Visual Basic Schneider

32 file1 contains: 1 4 7 file2 contains: 5 8 9
Question: file1 contains: file2 contains: 5 8 9 Private Sub Command1_Click() On Error GoTo fixMe x = "file1.txt" kill x Open x For Input As #1 Input #1, x Print x Close #1 Exit Sub fixMe: Print "welcome to error handling" x = "file2.txt" Resume End Sub welcome to error handling 5 Chapter 8 - Visual Basic Schneider

33 Chapter 8 - Visual Basic Schneider
Question: What is the output of the following code private sub command1_click() x = 12 y = 6 Print x / (y - 6); On Error GoTo myRoutine Print x / (y - 9); Exit Sub myRoutine: Print "Error"; y = y + 3 Resume End Sub Run time error (Division by zero) Chapter 8 - Visual Basic Schneider

34 Chapter 8 - Visual Basic Schneider
Example Chapter 8 - Visual Basic Schneider


Download ppt "Chapter 8 - Visual Basic Schneider"

Similar presentations


Ads by Google