Chapter 9: Sequential Access Files and Printing

Slides:



Advertisements
Similar presentations
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 24.1 Test-Driving the Ticket Information Application.
Advertisements

Chapter 6: The Repetition Structure
Chapter 11: Classes and Objects
Chapter 7: Sub and Function Procedures
IS 1181 IS 118 Introduction to Development Tools VB Chapter 06.
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic.NET, Second Edition.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 9 Structures and Sequential Access Files.
An Introduction to Programming with C++ Fifth Edition Chapter 13 Sequential Access Files.
Chapter 8: Manipulating Strings
Chapter 7: Sub and Function Procedures
MIS316 – BUSINESS APPLICATION DEVELOPMENT – Chapter 14 – Files and Streams 1Microsoft Visual C# 2012, Fifth Edition.
Chapter Three Using Variables and Constants Programming with Microsoft Visual Basic th Edition.
Chapter 8: String Manipulation
Programming with Microsoft Visual Basic th Edition
Saving and Loading Simple Text Files A sequential file stores characters one after the other like songs on a cassette tape. New characters are added to.
Programming with Microsoft Visual Basic th Edition CHAPTER SEVEN SUB AND FUNCTION PROCEDURES.
Programming with Microsoft Visual Basic 2012 Chapter 7: Sub and Function Procedures.
A First Program Using C#
Programming with Microsoft Visual Basic th Edition CHAPTER THREE USING VARIABLES AND CONSTANTS.
Programming with Microsoft Visual Basic 2012 Chapter 13: Working with Access Databases and LINQ.
Programming with Microsoft Visual Basic 2012 Chapter 12: Web Applications.
Microsoft Visual Basic 2005 CHAPTER 9 Using Arrays and File Handling.
Using Arrays and File Handling
Tutorial 61 List Box Control Can be used to display a set of choices from which the user can select only one You also can create multi-selection list boxes.
Programming with Microsoft Visual Basic 2008 Fourth Edition
Chapter 10: Structures and Sequential Access Files
Chapter Ten Structures and Sequential Access Files Programming with Microsoft Visual Basic th Edition.
Program Design and Coding
Microsoft Visual Basic 2012 CHAPTER THREE Program Design and Coding.
Microsoft Visual Basic 2010 CHAPTER THREE Program Design and Coding.
Creating Sequential Files, Adding Data, & Deleting Data.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Ticket Information Application Introducing Sequential-Access Files.
Chapter Ten Structures and Sequential Access Files Programming with Microsoft Visual Basic th Edition.
Reference: Lecturer Lecturer Reham O. Al-Abdul Jabba lectures for cap211 Files and Streams- I.
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Tutorial 9: Sequential Access Files and Printing1 Tutorial 9 Sequential Access Files and Printing.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
1.
Chapter 14: Files and Streams. 2Microsoft Visual C# 2012, Fifth Edition Files and the File and Directory Classes Temporary storage – Usually called computer.
Copyright © 2012 Pearson Education, Inc. Chapter 5 Loops, File, and Random Numbers.
Pascal Programming Today Chapter 11 1 Chapter 11.
Chapter Fourteen Access Databases and SQL Programming with Microsoft Visual Basic th Edition.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Three Using Variables and Constants.
Programming with Microsoft Visual Basic th Edition
Chapter 24 I’m Suffering from Information Overload (Access Databases) Clearly Visual Basic: Programming with Visual Basic nd Edition.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Ten Structures and Sequential Access Files.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Eight String Manipulation.
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.
Tutorial 81 Field, Record, Data File Field - a single item of information about a person, place, or thing Record - a group of related fields that contain.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 9 Structures and Sequential Access Files.
Chapter 23 The String Section (String Manipulation) Clearly Visual Basic: Programming with Visual Basic nd Edition.
Visual Basic.NET BASICS Lesson 14 Menus and Printing.
Programming with Microsoft Visual Basic 2012 Chapter 14: Access Databases and SQL.
Course ILT Using complex selection structures Unit objectives Include radio buttons and check boxes in an interface, create and call a user- defined Sub.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
1 Displaying Dialog Boxes Kashef Mughal. 2 Midterm Stats Here we go  Average was  Low was 116  High was 184  Mid Quarter Grade - check any.
Chapter 14: Sequential Access Files
Microsoft Visual Basic 2005: Reloaded Second Edition
Reading & writing to files
Topics Introduction to File Input and Output
Chapter 7 Files and Exceptions
CIS16 Application Development and Programming using Visual Basic.net
Chapter 3.5 Input and Output
Tutorial 9 Sequential Access Files and Printing
CIS16 Application Development and Programming using Visual Basic.net
Topics Introduction to File Input and Output
Topics Introduction to File Input and Output
Presentation transcript:

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 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) 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 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) 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 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) Figure 9-5: Procedure for using a sequential access file Programming with Microsoft Visual Basic .NET, Second Edition

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) 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 (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 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) 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 (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 (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 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 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) 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 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 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 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) Figure 9-14: User interface for the Friends application Programming with Microsoft Visual Basic .NET, Second Edition

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 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 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) 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 (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 (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 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) Figure 9-23: Interface for the PAO application Programming with Microsoft Visual Basic .NET, Second Edition

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 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 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) 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 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 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 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) 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 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) Figure 9-41: Pseudocode for the uiReportPrintDocument’s PrintPage event procedure Programming with Microsoft Visual Basic .NET, Second Edition

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