chapter 9 sequential access files and printing l.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Chapter 9: Sequential Access Files and Printing PowerPoint Presentation
Download Presentation
Chapter 9: Sequential Access Files and Printing

Loading in 2 Seconds...

play fullscreen
1 / 44

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


  • 236 Views
  • Uploaded on

Chapter 9: Sequential Access Files and Printing. Programming with Microsoft Visual Basic .NET, Second Edition. Sequential Access Files Lesson A Objectives. Declare StreamReader and StreamWriter variables Open a sequential access file Determine whether a sequential access file exists

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Chapter 9: Sequential Access Files and Printing' - harry


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
chapter 9 sequential access files and printing

Chapter 9: Sequential Access Files and Printing

Programming with Microsoft Visual Basic .NET, Second Edition

sequential access files lesson a objectives
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

Programming with Microsoft Visual Basic .NET, Second Edition

sequential access files lesson a objectives continued
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

Programming with Microsoft Visual Basic .NET, Second Edition

file types
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

file types continued
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

using sequential access files
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

using sequential access files continued
Using Sequential Access Files (continued)

Figure 9-5: Procedure for using a sequential access file

Programming with Microsoft Visual Basic .NET, Second Edition

declaring streamwriter and streamreader variables
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

declaring streamwriter and streamreader variables continued
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

declaring streamwriter and streamreader variables continued10
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

opening a sequential access file
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

opening a sequential access file continued
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

opening a sequential access file continued13
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

opening a sequential access file continued14
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

determining whether a file exists
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

writing information to a sequential access file
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

writing information to a sequential access file continued
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

reading information from a sequential access file
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

closing a sequential access file
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

the friends application
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

the friends application continued
The Friends Application (continued)

Figure 9-14: User interface for the Friends application

Programming with Microsoft Visual Basic .NET, Second Edition

the friends application continued22
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

records in a sequential access file lesson b objective
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

writing and reading records
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

writing and reading records continued
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

writing and reading records continued26
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

writing and reading records continued27
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

creating the pao application
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

creating the pao application continued
Creating the PAO Application (continued)

Figure 9-23: Interface for the PAO application

Programming with Microsoft Visual Basic .NET, Second Edition

coding the paoform load event procedure
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

coding the uiwritebutton click event procedure
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

coding the uidisplaybutton click event procedure
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

coding the uidisplaybutton click event procedure continued
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

printing a sequential access file lesson c objective
Printing a Sequential Access FileLesson 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

adding a printdocument control to the form
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

coding the print report button click event procedure
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

coding the print report button click event procedure continued
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

coding the printpage event procedure
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

coding the printpage event procedure continued
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

the e graphics drawstring method
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

the e graphics drawstring method continued
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

summary
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

summary continued
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

summary continued44
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