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

Slides:



Advertisements
Similar presentations
Introduction to File I/O How to read & write data to a disk file...
Advertisements

Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
VB Numbers and Strings School of Business Eastern Illinois University (Week 4, Monday 2/03/2003) © Abdou Illia, Spring 2003.
Chapter 11: Classes and Objects
Chapter 8 - Visual Basic Schneider1 Chapter 8 Sequential Files.
File Handling Advanced Higher Programming. What is a file? Up until now, any stored data within a program is lost when the program closes. A file is a.
Files Organisation sequential files. Readings u Schneider Chapter 8 u Shelly Cashman to 9.14; to 9.11 u Meyer to 2-37; 1995.
Alford Academy Business Education and Computing1 Advanced Higher Computing Based on Heriot-Watt University Scholar Materials File Handling.
Chapter 8 - Visual Basic Schneider1 Chapter 8 Sequential Files.
Types of LOOP Structures Do While ……. Loop Do Until …… Loop For …… Next loop.
Chapter 8 - Visual Basic Schneider1 Chapter 8 Sequential Files.
SUNY Morrisville-Norwich Campus-Week 12 CITA 130 Advanced Computer Applications II Spring 2005 Prof. Tom Smith.
Introduction to computers & Visual Basic School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 3, Monday 1/27/2003)
Two-Dimensional Arrays School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 11, Friday 4/04/2003)
User-Defined Data Types School of Business Eastern Illinois University © Abdou Illia, Fall 2002 (Week 13, Monday 11/18/2002)
Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition.
Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition.
To type the VB code behind the command button (named cmdPush), Double-Click on the Push Me (caption) command button As a result the Visual Basic Code Window.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Break Processing Please use speaker notes for additional information!
Array Processing: Exercises School of Business Eastern Illinois University © Abdou Illia, Spring 2002 (Week 10, Friday 3/28/2003)
Review for Exam 2 School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 10, Friday 3/21/2003) - IF Blocks - Do Loops - Select.
Programming Examples to Accompany Structure Topic Please use speaker notes for additional information!
Database Management School of Business Eastern Illinois University © Abdou Illia, Fall 2002 (Week 16, Monday 12/09/2002)
The Repetition Process in Visual Basic. The Repetition Process The capability to repeat one or more statements as many times as necessary is what really.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 7 Using Menus, Common Dialogs, Procedures, Functions, and Arrays.
Chapter 10: Structures and Sequential Access Files
Chapter Ten Structures and Sequential Access Files Programming with Microsoft Visual Basic th Edition.
Sub procedures School of Business Eastern Illinois University © Abdou Illia, Spring 2002 (Week 6, Friday 2/21/03)
Data files and databases. Need a control to browse to a file Standard controls for drive folder and list not much use The CommonDialogs control offers.
11-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
Chapter 7 File I/O 1. File, Record & Field 2 The file is just a chunk of disk space set aside for data and given a name. The computer has no idea what.
You can use Access forms to create an interface to your reports and queries. You can add: Buttons to initiate reports and queries Combo Boxes, List.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 7 Files.
Chapter 71 Repetition - Do Loops n A Loops, is used to repeat a sequence of statements a number of time n There are two loops commands in Visual Basic.
Visual Basic Programming
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
Delivery and other DO Examples Please use speaker notes for additional information!
I Power Higher Computing Software Development Development Languages and Environments.
Repetition Chapter 6 - Visual Basic Schneider 1  Loop Structure  Elements of a Loop Structure  Processing Lists of Data with Do Loops Chapter 6 -
‘Tirgul’ # 2 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #2.
Using Text Files in Excel File I/O Methods. Working With Text Files A file can be accessed in any of three ways: –Sequential access: By far the most common.
READING AND WRITING FILES. READING AND WRITING FILES SEQUENTIALLY  Two ways to read and write files  Sequentially and RA (Random Access  Sequential.
Pascal Programming Today Chapter 11 1 Chapter 11.
Files Tutor: You will need ….
Review for Final (Part 2) School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 15, Friday 5/2/2003)
Visual Basic I/O Programs (ProjRead1, ProjRead2, ProjWrite1, ProjPay) Please use speaker notes for additional information!
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Introduction to Files in VB Chapter 9.1, 9.3. Overview u Data Files  random access  sequential u Working with sequential files  open, read, write,
BACS 287 File-Based Programming. BACS 287 Data Hierarchy  Database - Collection of files, relationships, integrity information, etc  Files - All records.
Creating Menus Menu Bar – behaves like standard Windows menus Can be used in place of or in addition to buttons to execute a procedure Menu items are controls.
Compunet Corporation1 Programming with Visual Basic.NET Input and Output Files Lecture # 6 Tariq Ibn Aziz.
Subroutines (PrArith, Math,projCP1, PrAdrProc, PrAdrProcFunc) Please use speaker notes for additional information!
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Select Case Blocks School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 8, Monday 3/03/2003)
Visual Basic - Break Processing
Processing multiple files
Chapter 14: Sequential Access Files
User-Defined Data Types
Starting Out with Programming Logic & Design
Topics Introduction to File Input and Output
Chapter 7 Files and Exceptions
Visual Basic 6 Programming.
Text / Serial / Sequential Files
CIS16 Application Development and Programming using Visual Basic.net
Topics Introduction to File Input and Output
Chapter 8 - Visual Basic Schneider
Topics Introduction to File Input and Output
Chapter 11 Saving Data and Objects In Files
Text / Serial / Sequential Files
Presentation transcript:

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

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

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

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.

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

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

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.

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

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

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

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

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

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

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