Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sequential files School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 12, Monday 4/07/2003)

Similar presentations


Presentation on theme: "Sequential files School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 12, Monday 4/07/2003)"— Presentation transcript:

1

2 Sequential files School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 12, Monday 4/07/2003)

3 2 Sequential File ? Name Date-of-Birth Barbara 1972 Ringo 1960 Sylvester 1946 "Barbara",1972 "Ringo",1960 "Sylvester",1946 Employee.txt n Data file = collection of related data n Sequential file = Data file in which data items are saved and accessed in a sequential way n Record = Collection of data items about a single entity Barbara,1972 Example of record n Field: Refers to the entity that holds same type of data items Example: Name, Date-of-Birth

4 3 Creating Sequential Files n So far we have been using Windows’ Notepad to create Sequential files. n Sequential files can be created at run time in VB 1. Choose a file name (Ex: Employee.txt) 2. Choose a reference number (e.g: #1 or #2) 3. Open the file for Output 4. Write to the file 5. Close the file (e.g: Close #2) Steps for creating sequential files in VB Up to 255 characters, but 8.3 format names are usual A number from 1 to 511 Ex: Open "Employee.txt" For Output As #2 e.g: Write #2, "John", 1982

5 4 Creating Sequential files: Example Private Sub cmdCreateFile_Click() Open "Employee.txt" For Output As #1 Write #1, "Barbara", 1972 Write #1, " Ringo", 1960 Write #1, "Sylvester", 1945 Close #1 End Sub Reference number File name Write to the file Close the file When an existing file is opened for Output, its existing content is erased and a new empty file is created.

6 5 Creating Sequential files: Problem 1 n Suppose that data should be entered using the following form and the program below. What will be the content of Employee.txt if these three records are entered. Private Sub cmdAddRecord_Click() Open "Employee.txt" For Output As #1 If (txtName.Text <> "") And (txtDate.Text <> "") Then Write #1, txtName.Text, Val(txtDate.Text) txtName.Text = "" txtDate.Text = "" txtName.SetFocus End If Close #1 End Sub When an existing file is opened for Output, its existing content is erased and a new empty file is created. Name Date-of-Birth Barbara 1972 Ringo 1960 Sylvester 1946

7 6 Creating Sequential files: Problem 1 n Problem can be solved if Employee.txt is opened in the Load_Form event procedure, and closed in an Exit_Click event procedure. Write the appropriate program. Private Sub Form_Load() End Sub Private Sub cmdAddRecord_Click() Write #1, txtName.Text, Val(txtDate.Text) txtName.Text = "" txtDate.Text = "" txtName.SetFocus End Sub Private Sub cmdExit_Click() End Sub

8 7 Adding Items to a Sequential File 1. Choose a reference number for the file 2. Open the file for Append 3. Write to the end of the file using Write statements 4. Close the file using a Close statement Note: a)A file is opened in Append mode in order to add new records at the end of its existing content. b)If the file doesn’t exist, a new file is created.

9 8 Adding Items to a Sequential File: Problem 2 n Suppose that data should be entered using the following form and the program below. What will be the content of Employee.txt if these three records are entered. Private Sub cmdAddRecord_Click() Open "Employee.txt" For Append As #1 If (txtName.Text <> "") And (txtDate.Text <> "") Then Write #1, txtName.Text, Val(txtDate.Text) txtName.Text = "" txtDate.Text = "" txtName.SetFocus End If Close #1 End Sub Name Date-of-Birth Barbara 1972 Ringo 1960 Sylvester 1946

10 9 Sequential files: Problem 3 n What is the effect of the following program? Private Sub cmdDisplay_Click() Dim g As String Open "Greeting.txt" For Input As #1 Open "Welcome.txt" For Output As #2 Do While Not EOF(1) Input #1, g If g "Aloha" Write #2, g End If Loop Close End Sub "Hello" "Aloha" "Bonjour" Greeting.txt

11 10 Sequential files: rules to know n A sequential file could be opened in one of the following 3 modes: Input, Output, Append n Input mode is used: – To open an existing file and – to read its existing content using Input # statements Open “FileSpecification” For Input As #RefNumber : Input #RefNumber, DataItemsList : Close [#RefNumber] FileSpecification : Specifies a file name — may include directory or folder, and drive RfeNumber: Specifies the file’s Reference number DataItemsList: Comma-delimited list of variables that are assigned values read from the file

12 11 Sequential files: rules to know n Output mode. – Is used to open a file that doesn’t exist. So, a new file is created – If the Output mode is used to open a file that already exist, its existing content is erased and a new empty file is created – With the Output mode, Write # statements are used to write data to the opened file Open “FileSpecification” For Output As #RefNumber : Write #RefNumber, OutputList : Close [#RefNumber] FileSpecification : Specifies a file name — may include directory or folder, and drive RfeNumber: Specifies the file’s Reference number DataItemsList: One or more comma-delimited numeric expressions or string expressions to write to a file

13 12 Sequential files: rules to know n Append mode. – Is used to open an existing file in order to add new records at the end of its existing content. – If the Append mode is used to open a file that doesn’t exist, a new file is created. – With the Append mode, Write # statements are used to add data to the opened file Open “FileSpecification” For Append As #RefNumber : Write #RefNumber, OutputList : Close [#RefNumber] FileSpecification : Specifies a file name — may include directory or folder, and drive RfeNumber: Specifies the file’s Reference number DataItemsList: One or more comma-delimited numeric expressions or string expressions to write to a file

14 13 Exercises 1)Suppose that Employee.txt already exist and contains 5 records. What will be the content of the file Employee.txt if the three records (shown above) are entered using the following interface? Name Date-of-Birth Barbara 1972 Ringo 1960 Sylvester 1946 Private Sub cmdAddRecord_Click() Open "Employee.txt" For Output As #1 Write #1, txtName.Text, Val(txtDate.Text) txtName.Text = "" txtDate.Text = "" txtName.SetFocus Close #1 End Sub a) 8 records (the existing 5 records + the 3 records entered) b) 3 records (the new 3 records entered when the program was executed) c) 1 record (the 1st new record entered when the program is executed) d) 1 record (the last new record entered when the program is executed) e) None of the above

15 14 Exercises n Suppose that the file Greeting.txt has the content shown below. Suppose too that the file Welcome.txt already exist and contains 2 records. What will be its content if the following program is executed? Private Sub cmdDisplay_Click() Dim DataItem As String Open “Greeting.txt” For Input As #1 Open “Welcome.txt” For Output As #2 Do While Not EOF(1) Input #1, DataItem If DataItem “Aloha” Write #2, DataItem End If Loop Close End Sub a) 4 records (the existing 2 records + the 2 new records written by the program) b) 2 records (the 2 new records written by the program) c) None of the above "Hello" "Aloha" "Bonjour" Greeting.txt


Download ppt "Sequential files School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 12, Monday 4/07/2003)"

Similar presentations


Ads by Google