Top Banner
Sequential Access Files and Error Handling Chapter Microsoft Visual Basic .NET: Reloaded 1 This is from an old VB text but coding conventions still apply
48

This is from an old VB text but coding conventions still apply

Jan 22, 2016

Download

Documents

La_Vey

This is from an old VB text but coding conventions still apply. 1. 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 - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: This is from an old VB text but coding conventions still apply

Sequential Access Files and Error Handling

Chapter Microsoft Visual Basic .NET: Reloaded

1

This is from an old VB text but coding conventions still apply

Page 2: This is from an old VB text but coding conventions still apply

2Microsoft Visual Basic .NET: Reloaded

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

Page 3: This is from an old VB text but coding conventions still apply

3Microsoft Visual Basic .NET: Reloaded

Objectives (continued)

• Read information from a sequential access file

• Test for the end of a sequential access file

• Close a sequential access file

• Handle exceptions using a Try/Catch block

• Write records to a sequential access file

• Read records from a sequential access file

Page 4: This is from an old VB text but coding conventions still apply

4Microsoft Visual Basic .NET: Reloaded

File Types

• Output files• Files to which output is written

• Store output produced by the application

• Input Files• Files that are read by the computer

• Application uses information stored in files

• Sequential• Often referred to as “text files”

• Binary and Random• Not covered in this text

Page 5: This is from an old VB text but coding conventions still apply

5Microsoft Visual Basic .NET: Reloaded

Using Sequential Access Files

Page 6: This is from an old VB text but coding conventions still apply

6Microsoft Visual Basic .NET: Reloaded

HOW TO…

Page 7: This is from an old VB text but coding conventions still apply

7Microsoft Visual Basic .NET: Reloaded

Declaring StreamWriter and StreamReader Variables

• StreamWriter object

• Writes a sequence of characters to a sequential access file

• Referred to as a “stream of characters”

• Also referred to as a “stream”

• StreamReader object

• Reads a stream (sequence of characters) to a sequential access file

Page 8: This is from an old VB text but coding conventions still apply

8Microsoft Visual Basic .NET: Reloaded

HOW TO…

Page 9: This is from an old VB text but coding conventions still apply

9Microsoft Visual Basic .NET: Reloaded

Opening a Sequential Access file

• Declare variable of StreamWriter or StreamReader object datatype• Then use variable to refer to object and file in

the program

• OpenText method• Opens an existing file for input

• CreateText method• Creates a new empty file for output

• AppendText method• Appends data to the end of an existing

sequential access file

Page 10: This is from an old VB text but coding conventions still apply

10Microsoft Visual Basic .NET: Reloaded

HOW TO…

Page 11: This is from an old VB text but coding conventions still apply

11Microsoft Visual Basic .NET: Reloaded

Opening a Sequential Access file (continued)

Page 12: This is from an old VB text but coding conventions still apply

12Microsoft Visual Basic .NET: Reloaded

Determining Whether a File Exists

Page 13: This is from an old VB text but coding conventions still apply

13Microsoft Visual Basic .NET: Reloaded

Writing Information to a Sequential Access File

• Write method • Positions file pointer at end of last character it

writes to the file

• WriteLine method• Positions file pointer at beginning of next line in

the file by appending a line termination character (carriage return, line feed)

• Space function• Use to write a specific number of spaces to file

Page 14: This is from an old VB text but coding conventions still apply

14Microsoft Visual Basic .NET: Reloaded

HOW TO…

Page 15: This is from an old VB text but coding conventions still apply

15Microsoft Visual Basic .NET: Reloaded

Aligning Columns of Information

• PadLeft(length, [character])

• Pads at beginning of string with a specified character until string is a specified length

• Character is optional and default is a space

• PadRight(length, [character])

• Same as above only pads at end of string

Page 16: This is from an old VB text but coding conventions still apply

16Microsoft Visual Basic .NET: Reloaded

HOW TO…

Page 17: This is from an old VB text but coding conventions still apply

17Microsoft Visual Basic .NET: Reloaded

Reading Information from a Sequential Access file

• ReadLine method• Reads a line of text from sequential access file

• A line is a sequence of characters followed by the line termination character

• Peek method• Peeks into a file to see if file contains another

character to read

• Returns the number -1 if no more data in file

• Typically used within a loop to prevent a file read error

Page 18: This is from an old VB text but coding conventions still apply

18Microsoft Visual Basic .NET: Reloaded

HOW TO…

Page 19: This is from an old VB text but coding conventions still apply

19Microsoft Visual Basic .NET: Reloaded

Closing a Sequential Access File

• Close method• Closes file associate with stream variable

Page 20: This is from an old VB text but coding conventions still apply

20Microsoft Visual Basic .NET: Reloaded

The Friends Application

• Allows user to write names of his or her friends to a sequential access file

• Allows names to be read from above file

Page 21: This is from an old VB text but coding conventions still apply

21Microsoft Visual Basic .NET: Reloaded

The Friends Application (continued)

Page 22: This is from an old VB text but coding conventions still apply

22Microsoft Visual Basic .NET: Reloaded

The Friends Application (continued)

Page 23: This is from an old VB text but coding conventions still apply

23Microsoft Visual Basic .NET: Reloaded

The Friends Application (continued)

Page 24: This is from an old VB text but coding conventions still apply

24Microsoft Visual Basic .NET: Reloaded

HOW TO…

Page 25: This is from an old VB text but coding conventions still apply

25Microsoft Visual Basic .NET: Reloaded

The Friends Application (continued)

• btnRead_Click event • Code causes name read from file

to be displayed in a message box

Page 26: This is from an old VB text but coding conventions still apply

26Microsoft Visual Basic .NET: Reloaded

Using a Try/Catch Block

• Try statement• Use to catch (or trap) an exception

• Exception is an error that occurs while program is running

• Catch statement• Use to have computer take appropriate action• More than one catch can occur after a try

• Multiple catch allow for trapping of different types of errors

• Try/Catch block• Block of code that uses both Try and Catch

statements

Page 27: This is from an old VB text but coding conventions still apply

27Microsoft Visual Basic .NET: Reloaded

HOW TO…

Page 28: This is from an old VB text but coding conventions still apply

28Microsoft Visual Basic .NET: Reloaded

HOW TO…(continued)

Page 29: This is from an old VB text but coding conventions still apply

29Microsoft Visual Basic .NET: Reloaded

Using a Try/Catch Block (continued)

Page 30: This is from an old VB text but coding conventions still apply

30Microsoft Visual Basic .NET: Reloaded

Using a Try/Catch Block (continued)

Page 31: This is from an old VB text but coding conventions still apply

31Microsoft Visual Basic .NET: Reloaded

Writing and Reading Records

• Field

• A single item of information about a person, place, or thing

• Examples: Name, salary, price, age

• Record

• One or more related fields that contain all the necessary data about a specific person, place, or thing

Page 32: This is from an old VB text but coding conventions still apply

32Microsoft Visual Basic .NET: Reloaded

HOW TO…

Page 33: This is from an old VB text but coding conventions still apply

33Microsoft Visual Basic .NET: Reloaded

HOW TO…

Page 34: This is from an old VB text but coding conventions still apply

34Microsoft Visual Basic .NET: Reloaded

Programming Example - PAO Application

• Application allows user to enter the political party and age

• The input information is saved to a sequential access file

• In addition, the application calculates and displays the number of voters in each political party

Page 35: This is from an old VB text but coding conventions still apply

35Microsoft Visual Basic .NET: Reloaded

TOE Chart

Page 36: This is from an old VB text but coding conventions still apply

36Microsoft Visual Basic .NET: Reloaded

TOE Chart (continued)

Page 37: This is from an old VB text but coding conventions still apply

37Microsoft Visual Basic .NET: Reloaded

User Interface

Page 38: This is from an old VB text but coding conventions still apply

38Microsoft Visual Basic .NET: Reloaded

Objects, Properties, and Settings

Page 39: This is from an old VB text but coding conventions still apply

39Microsoft Visual Basic .NET: Reloaded

Objects, Properties, and Settings (continued)

Page 40: This is from an old VB text but coding conventions still apply

40Microsoft Visual Basic .NET: Reloaded

Tab Order

Page 41: This is from an old VB text but coding conventions still apply

41Microsoft Visual Basic .NET: Reloaded

PseudocodebtnExit Click event procedure

close the applicationbtnWrite Click event procedure

open pao.txt file for appendif txtparty control contains D, R, or I

Write contents of txtParty control, a comma, and txtAge to pao.txt file

elseDisplay message prompting user to enter D,R, or I

End ifclose the pao.txt fileClear contents of txtParty and txtAge controlsSend focus to txtParty controlInclude a Try/Catch block to catch general errors displaying error description in a messagebox

Page 42: This is from an old VB text but coding conventions still apply

42Microsoft Visual Basic .NET: Reloaded

Pseudocode (continued)

btnDisplay click event procedureopen pao.txt file for inputrepeat until no more characters to read

read record from fileif first character is letter D

add 1 to Democrat counterelse if first character is R

add 1 to Republican counterelse add 1 to Independent counterend if

end tryclose pao.txt file

Page 43: This is from an old VB text but coding conventions still apply

43Microsoft Visual Basic .NET: Reloaded

Pseudocode (continued)

btnDisplay Click event (continued)

Display Democrat counter value in lblTotalDem

Display Republican counter value in lblTotalRep

Display Independent counter value in lblTotalInd

Include Try/Catch block with 2 Catch sections

The first catches FileNotFoundException and displays “Cannot locate the pao.txt file” in a messagebox

The second catches general errors and displays a description of the error in a messagebox

Page 44: This is from an old VB text but coding conventions still apply

44Microsoft Visual Basic .NET: Reloaded

Code

Page 45: This is from an old VB text but coding conventions still apply

45Microsoft Visual Basic .NET: Reloaded

Code (continued)

Page 46: This is from an old VB text but coding conventions still apply

46Microsoft Visual Basic .NET: Reloaded

Summary• Information in a sequential access file is

accessed in consecutive order (from beginning to end)

• Use the StreamWriter object to write a sequence of characters (stream) to a sequential access file

• Use a StreamReader object to read a stream from a sequential access file

• Exists method returns a boolean value indicating whether file exists

Page 47: This is from an old VB text but coding conventions still apply

47Microsoft Visual Basic .NET: Reloaded

Summary (continued)• Write method writes but does not write a line

termination characters• WriteLine method appends a line termination

character• ReadLine method reads a line of text • Peek method determines whether file has

more data to be read• Space function writes specific number of

spaces to file• PadLeft and PadRight pad characters to file to

align columns of information

Page 48: This is from an old VB text but coding conventions still apply

48Microsoft Visual Basic .NET: Reloaded

Summary (continued)

• To prevent loss of data, call the Close method• Use a Try/Catch block to catch an exception

(error) and to take appropriate action when exception occurs

• Use a general Catch statement as the last Catch statement to handle unexpected errors

• Display exception using syntax variablename.Message where variablename is the name of variable in catch statement

• Sequential files are used to store fields and records