Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 9: Sequential Access Files and Printing

Similar presentations


Presentation on theme: "Chapter 9: Sequential Access Files and Printing"— Presentation transcript:

1 Chapter 9: Sequential Access Files and Printing
Programming with Microsoft Visual Basic .NET, Second Edition

2 Sequential Access Files Lesson A Objectives
Declare StreamReader and StreamWriter variables Open a sequential access file Determine whether a sequential access file exists Write information to a sequential access file Align the text written to a sequential access file Programming with Microsoft Visual Basic .NET, Second Edition

3 Sequential Access Files Lesson A Objectives (continued)
Read information from a sequential access file Determine whether the computer has finished reading a sequential access file Close a sequential access file Programming with Microsoft Visual Basic .NET, Second Edition

4 File Types Files to which information is written are called output files The files store the output produced by an application Files that are read by the computer are called input files An application uses the information in these files as input Programming with Microsoft Visual Basic .NET, Second Edition

5 File Types (continued)
Here is a list of different file types: Sequential access files Random access files Binary access files Programming with Microsoft Visual Basic .NET, Second Edition

6 Using Sequential Access Files
A sequential access file is often referred to as a text file, because it is composed of lines of text Sequential access files are similar to cassette tapes in that each line in the file, like each song on a cassette tape, is both stored and retrieved in consecutive order (sequentially) Programming with Microsoft Visual Basic .NET, Second Edition

7 Using Sequential Access Files (continued)
Figure 9-5: Procedure for using a sequential access file Programming with Microsoft Visual Basic .NET, Second Edition

8 Declaring StreamWriter and StreamReader Variables
Use a StreamWriter object to write a sequence of characters to a sequential access file The sequence of characters is referred to as a stream of characters (or a stream) Use a StreamReader object to read a stream (sequence of characters) from a sequential access file Programming with Microsoft Visual Basic .NET, Second Edition

9 Declaring StreamWriter and StreamReader Variables (continued)
Before creating the appropriate object, first declare an object variable to store the address of the object in the computer’s internal memory Use a StreamWriter variable to store the address of a StreamWriter object Use a StreamReader variable to store the address of a StreamReader object Programming with Microsoft Visual Basic .NET, Second Edition

10 Declaring StreamWriter and StreamReader Variables (continued)
Figure 9-6: Syntax and examples of declaring StreamWriter and StreamReader variables Programming with Microsoft Visual Basic .NET, Second Edition

11 Opening a Sequential Access File
The OpenText method opens an existing sequential access file for input and allows the computer to read the information stored in the file Use the CreateText method to create a new, empty sequential access file to which data can be written Use the AppendText method to add data to the end of an existing sequential access file Programming with Microsoft Visual Basic .NET, Second Edition

12 Opening a Sequential Access File (continued)
Figure 9-7: Syntax and examples of opening a sequential access file Programming with Microsoft Visual Basic .NET, Second Edition

13 Opening a Sequential Access File (continued)
Figure 9-7: Syntax and examples of opening a sequential access file (continued) Programming with Microsoft Visual Basic .NET, Second Edition

14 Opening a Sequential Access File (continued)
Figure 9-8: Position of the file pointer when files are opened for input, output, and append Programming with Microsoft Visual Basic .NET, Second Edition

15 Determining Whether a File Exists
Avoid errors by using the Exists method Syntax: IO.File.Exists(filename) The Exists method returns the Boolean value True if filename exists; otherwise, it returns the Boolean value False Programming with Microsoft Visual Basic .NET, Second Edition

16 Writing Information to a Sequential Access File
Use either the Write method or the WriteLine method to write information to a sequential access file Write method Positions the file pointer at the end of the last character it writes to the file Syntax: variablename.Write(data) Programming with Microsoft Visual Basic .NET, Second Edition

17 Writing Information to a Sequential Access File (continued)
WriteLine method Positions the file pointer at the beginning of the next line in the file Syntax: variablename.WriteLine(data) Programming with Microsoft Visual Basic .NET, Second Edition

18 Reading Information from a Sequential Access File
ReadLine method Read a line of text from the file Syntax: variablename.ReadLine() Peek method Looks to see if there is another character to read Syntax: variablename.Peek() Programming with Microsoft Visual Basic .NET, Second Edition

19 Closing a Sequential Access File
Close method: closes the file when you are finished Syntax: variablename.Close() Variablename is the name of either a StreamReader or StreamWriter variable Programming with Microsoft Visual Basic .NET, Second Edition

20 The Friends Application
Allows the user to: Write the names of his or her friends to a sequential access file Read the names from the file Programming with Microsoft Visual Basic .NET, Second Edition

21 The Friends Application (continued)
Figure 9-14: User interface for the Friends application Programming with Microsoft Visual Basic .NET, Second Edition

22 The Friends Application (continued)
Figure 9-15: Pseudocode for the Write to File and Read from File buttons Programming with Microsoft Visual Basic .NET, Second Edition

23 Records In a Sequential Access File Lesson B Objective
Write records to a sequential access file Read records from a sequential access file Programming with Microsoft Visual Basic .NET, Second Edition

24 Writing and Reading Records
A sequential access file can be used to store fields and records A field is a single item of information about a person, place, or thing A record is one or more related fields that contain all of the necessary data about a specific person, place, or thing Programming with Microsoft Visual Basic .NET, Second Edition

25 Writing and Reading Records (continued)
When writing records to a sequential access file, you typically write each record on a separate line in the file If the records contain more than one field, you can separate each field with a special character, such as a comma or the number symbol (#) Programming with Microsoft Visual Basic .NET, Second Edition

26 Writing and Reading Records (continued)
Figure 9-21: Examples of writing a record to a sequential access file Programming with Microsoft Visual Basic .NET, Second Edition

27 Writing and Reading Records (continued)
Figure 9-22: Examples of reading records from a sequential access file Programming with Microsoft Visual Basic .NET, Second Edition

28 Creating the PAO Application
Application for the PAO (Political Awareness Organization) should allow the organization’s secretary to: Save (to a sequential access file) a voter’s political party and age Tabulate the number of Democrats, Republicans, and Independents entered in the file Print the contents of the file Programming with Microsoft Visual Basic .NET, Second Edition

29 Creating the PAO Application (continued)
Figure 9-23: Interface for the PAO application Programming with Microsoft Visual Basic .NET, Second Edition

30 Coding the PaoForm Load Event Procedure
Figure 9-25: Pseudocode for the PaoForm’s Load event procedure Programming with Microsoft Visual Basic .NET, Second Edition

31 Coding the uiWriteButton Click Event Procedure
Figure 9-30: Pseudocode for the uiWriteButton’s Click event procedure Programming with Microsoft Visual Basic .NET, Second Edition

32 Coding the uiDisplayButton Click Event Procedure
Figure 9-34: Pseudocode for the uiDisplayButton’s Click event procedure Programming with Microsoft Visual Basic .NET, Second Edition

33 Coding the uiDisplayButton Click Event Procedure (continued)
Figure 9-34: Pseudocode for the uiDisplayButton’s Click event procedure (continued) Programming with Microsoft Visual Basic .NET, Second Edition

34 Printing a Sequential Access File Lesson C Objective
Add a PrintDocument control to a form Print text using the Print and e.Graphics.DrawString methods Code a PrintDocument control’s PrintPage event procedure Programming with Microsoft Visual Basic .NET, Second Edition

35 Adding a PrintDocument Control to the Form
To complete the PAO application, you need to: Add a PrintDocument control to the form Code the Print Report button’s Click event procedure and the PrintDocument control’s PrintPage event procedure Programming with Microsoft Visual Basic .NET, Second Edition

36 Coding the Print Report Button Click Event Procedure
The Print Report button’s Click event procedure is responsible for using the uiReportPrintDocument control to print the contents of the pao.txt sequential access file The Print method causes the PrintDocument control’s PrintPage event to occur You use the PrintPage event to indicate the information you want to print, as well as how you want the information to appear in the printout Programming with Microsoft Visual Basic .NET, Second Edition

37 Coding the Print Report Button Click Event Procedure (continued)
Figure 9-39: Pseudocode for the Print Report button’s Click event procedure Programming with Microsoft Visual Basic .NET, Second Edition

38 Coding the PrintPage Event Procedure
The PrintPage event procedure should print the contents of the pao.txt file in a report format The report should contain a report header and two columns of information The first column should list the party The second column should list the age Programming with Microsoft Visual Basic .NET, Second Edition

39 Coding the PrintPage Event Procedure (continued)
Figure 9-41: Pseudocode for the uiReportPrintDocument’s PrintPage event procedure Programming with Microsoft Visual Basic .NET, Second Edition

40 The e.Graphics.DrawString Method
You use the e.Graphics.DrawString method to print text on the printer Some print fonts are proportionally spaced, while others are fixed-spaced, often referred to as mono-spaced Programming with Microsoft Visual Basic .NET, Second Edition

41 The e.Graphics.DrawString Method (continued)
Fixed-spaced fonts use the same amount of space to print each character Proportionally spaced fonts use varying amounts of space to print characters Programming with Microsoft Visual Basic .NET, Second Edition

42 Summary To declare a StreamWriter or StreamReader variable, use the syntax: {Dim | Private} variablename As IO.objecttype To open a sequential access file for input, use the syntax: IO.File.OpenText(filename) To open a sequential access file for append, use the syntax: IO.File.AppendText(filename) To open a sequential access file for output, use the syntax: IO.File.CreateText(filename) Programming with Microsoft Visual Basic .NET, Second Edition

43 Summary (continued) To write a record to a sequential access file, you typically write each record on a separate line in the file If the records contain more than one field, separate each field with a special character To read a record from a sequential access file, use the ReadLine method to read a line of text from the file; the line of text is the record Programming with Microsoft Visual Basic .NET, Second Edition

44 Summary (continued) To print text within a Visual Basic .NET application: Include a PrintDocument control in the application Use the Print method to print the document Use the PrintPage event procedure to indicate the information you want to print and how you want the information to appear in the printout Use the syntax e.Graphics.DrawString(string, font, Brushes.Black, horizontalPosition, verticalPosition) to print the document Programming with Microsoft Visual Basic .NET, Second Edition


Download ppt "Chapter 9: Sequential Access Files and Printing"

Similar presentations


Ads by Google