Chapter 9: Sequential Access Files and Printing - PowerPoint PPT Presentation

1 / 44
About This Presentation
Title:

Chapter 9: Sequential Access Files and Printing

Description:

Sequential access files are similar to cassette tapes in that each line in the ... each song on a cassette tape, is both stored and retrieved in consecutive ... – PowerPoint PPT presentation

Number of Views:101
Avg rating:3.0/5.0
Slides: 45
Provided by: david2324
Category:

less

Transcript and Presenter's Notes

Title: Chapter 9: Sequential Access Files and Printing


1
Chapter 9 Sequential Access Files and Printing
Programming with Microsoft Visual Basic .NET,
Second Edition
2
Sequential Access FilesLesson 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

3
Sequential Access FilesLesson 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

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

5
File Types (continued)
  • Here is a list of different file types
  • Sequential access files
  • Random access files
  • Binary access files

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)

7
Using Sequential Access Files (continued)
Figure 9-5 Procedure for using a sequential
access file
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

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 computers 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

10
Declaring StreamWriter and StreamReader Variables
(continued)
Figure 9-6 Syntax and examples of declaring
StreamWriter and StreamReader variables
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

12
Opening a Sequential Access File (continued)
Figure 9-7 Syntax and examples of opening a
sequential access file
13
Opening a Sequential Access File (continued)
Figure 9-7 Syntax and examples of opening a
sequential access file (continued)
14
Opening a Sequential Access File (continued)
Figure 9-8 Position of the file pointer when
files are opened for input, output, and append
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

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)

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)

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()

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

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

21
The Friends Application (continued)
Figure 9-14 User interface for the Friends
application
22
The Friends Application (continued)
Figure 9-15 Pseudocode for the Write to File and
Read from File buttons
23
Records In a Sequential Access File Lesson B
Objective
  • Write records to a sequential access file
  • Read records from a sequential access file

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

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 ()

26
Writing and Reading Records (continued)
Figure 9-21 Examples of writing a record to a
sequential access file
27
Writing and Reading Records (continued)
Figure 9-22 Examples of reading records from a
sequential access file
28
Creating the PAO Application
  • Application for the PAO (Political Awareness
    Organization) should allow the organizations
    secretary to
  • Save (to a sequential access file) a voters
    political party and age
  • Tabulate the number of Democrats, Republicans,
    and Independents entered in the file
  • Print the contents of the file

29
Creating the PAO Application (continued)
Figure 9-23 Interface for the PAO application
30
Coding the PaoForm Load Event Procedure
Figure 9-25 Pseudocode for the PaoForms Load
event procedure
31
Coding the uiWriteButton Click Event Procedure
Figure 9-30 Pseudocode for the uiWriteButtons
Click event procedure
32
Coding the uiDisplayButton Click Event Procedure
Figure 9-34 Pseudocode for the uiDisplayButtons
Click event procedure
33
Coding the uiDisplayButton Click Event Procedure
(continued)
Figure 9-34 Pseudocode for the uiDisplayButtons
Click event procedure (continued)
34
Printing a Sequential Access FileLesson C
Objective
  • Add a PrintDocument control to a form
  • Print text using the Print and e.Graphics.DrawStri
    ng methods
  • Code a PrintDocument controls PrintPage event
    procedure

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 buttons Click event
    procedure and the PrintDocument controls
    PrintPage event procedure

36
Coding the Print Report Button Click Event
Procedure
  • The Print Report buttons 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
    controls 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

37
Coding the Print Report Button Click Event
Procedure (continued)
Figure 9-39 Pseudocode for the Print Report
buttons Click event procedure
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

39
Coding the PrintPage Event Procedure (continued)
Figure 9-41 Pseudocode for the
uiReportPrintDocuments PrintPage event procedure
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

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

42
Summary
  • To declare a StreamWriter or StreamReader
    variable, use the syntaxDim 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)

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

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
Write a Comment
User Comments (0)
About PowerShow.com