Chapter 10 Sequential Files and Structures. Class 10: Sequential Files Work with different types of...

Preview:

Citation preview

Chapter 10

Sequential Files and Structures

Class 10: Sequential Files

• Work with different types of sequential files

• Read sequential files based on the type of data stored in a text file

• Write sequential files

• Use structures to store and group data together

Introduction to Processing Textual Data

• One way to process textual files is from beginning to end using sequential access

• This type of file is called a sequential file• Sequential files can be categorized into roughly three

types– Free-form files have no particular format

– Fields in a delimited file are separated with a special character called a delimiter

– In a fixed-field file, each field occupies the same character positions

The Format of Freeform Files

• Freeform files have no particular format

• The file contains one or more textual characters appearing on one or many lines

• The number of lines in the file is not significant

• Freeform files can be read character-by-character or all at once

The Format of Delimited Files

• All delimited files have a well-defined structure and the following characteristics:– Sequential files are separated into lines ending

with a hard return– Each line in a sequential file is called a record– Each record contains one or more fields– A delimiter, often a comma, separates fields

• A delimiter can consist of multiple characters

Figure 10-6:Delimited Sequential File

The Format of Fixed-field Files

• Fixed-field files contain textual characters

• A specific character position marks the start and end of each field

• Each record is terminated with a hard return

Figure 10-7:Fixed-field File

Opening and Closing Sequential Files with the StreamReader

• The StreamReader and StreamWriter classes belong to the System.IO namespace

• Opening a sequential file establishes a connection between an application and a physical file

• Sequential files can be read one character at a time, one line at a time, or the entire file can be read at once

• Sequential files are typically read into a string or an array

• Closing a file disconnects the application from the file

The StreamReader Constructor (Example)

• Open a file named "C:\Demo.txt"

Dim CurrentReader As _

System.IO.StreamReader = _

New System.IO.StreamReader( _

"C:\Demo.txt")

Closing a Sequential File

• The Close method closes a sequential file

• Always close files when processing is complete to prevent loss of data

• Open files also consume system resources

• Example:CurrentReader.Close()

Reading the Entire Contents of a File (Example)

• Read the file named "C:\Demo.txt"

Dim CurrentString As String

Dim CurrentReader As New _

StreamReader("C:\Demo.txt")

CurrentString = _

CurrentReader.ReadToEnd()

CurrentReader.Close()

Reading a Sequential File Character by Character

• It's possible to read a sequential file character-by-character

• Steps:– Call the Read method to perform a priming read

– In a loop, test that a character was read• (Check that end-of-file was not reached)

• Process the character

• Read the next character

Reading a Sequential File Character by Character (Example)

Dim CurrentChar As IntegerDim CurrentReader As New _ StreamReader("C:\Demo.txt")CurrentChar = CurrentReader.Read()Do Until CurrentChar = –1 ' Statements to process the character. CurrentChar = CurrentReader.Read()LoopCurrentReader.Close()

Reading a Sequential File One Record at a Time

• Delimited files are processed using the following steps:– Call the ReadLine method to perform a

priming read– Using a Do loop, process the record that was

read– Read the next record– After reading all records, close the file

Reading a Sequential File as a List of Records (Example)

Dim CurrentReader As New _ System.IO.StreamReader("C:\Demo.txt")

Dim CurrentRecord As StringCurrentRecord = CurrentReader.ReadLine()Do Until CurrentRecord = Nothing ' Statements to process the current record.

CurrentRecord = CurrentReader.ReadLine()LoopCurrentReader.Close()

Writing a Sequential File

• The StreamWriter class of the System.IO namespace writes a sequential file

• The constructor accepts one argument – the file to write

• Example:Dim CurrentWriter As New _

System.IO.StreamWriter("C:\Demo.txt")

' Statements to write the file.

CurrentWriter.Close()

The StreamWriter Class (Members)

• The NewLine property contains the character(s) that mark the end of the line

• The Close method closes the sequential file– It's imperative to close a sequential file once writing is

complete to prevent loss of data

• The Write method writes a character or array of characters

• The WriteLine method writes data terminated by the character(s) stored in the NewLine property

Writing a Freeform File

• A freeform file can be written all at once as follows:

Dim StringData As String = "Freeform text"

Dim CurrentWriter As New _

System.IO.StreamWriter("C:\Demo.txt")

CurrentWriter.Write(StringData)

CurrentWriter.Close()

Writing a Delimited File

• The steps to write delimited file are as follows:– Create an instance of the StreamWriter

class– Using a For loop, call the WriteLine

method to write each record– Call the Close method when writing is

complete

Writing a Delimited File (Example)

• Write an array of IntegersPublic Shared Sub WriteIntegerList( _ ByRef argArray() As Integer, _

ByVal argFile As String) Dim CurrentStream As New StreamWriter(argFile) Dim CurrentIndex As Integer For CurrentIndex = 0 To argArray.GetUpperBound(0) CurrentStream.Write(argArray(CurrentIndex)) If CurrentIndex <> _

argArray.GetUpperBound(0) Then CurrentStream.Write(",") End If Next CurrentStream.Close()End Function

Introduction to StructuresIntroduction to Structures

• A structure is used to store related data items, and groups them together logically

• A structure can take the place of multiple individual variables

• The Structure keyword is used to declare a structure– Once declared, variables can be declared

having the declared data type

Structures (Syntax)

[Public | Friend] Structure name

variableDeclarations [procedureDeclarations]End Structure

Structures(Syntax, continued)

• The Structure and End Structure keywords mark the beginning and end of a structure block

• The Public and Friend keywords mark the accessibility of the structure

• name defines the structure name– Use Pascal case for structure names

• variableDeclarations contains the structure members

• procedureDeclarations contains procedures appearing in the structure

Structures (Example)

• Declare a structure named Contact with four membersPublic Structure Contact

Public FirstName As String

Public LastName As String

Public TelephoneNumber As String

Public DateAdded As DateTime

End Structure

Storing and Retrieving Data From a Structure

• Assignment statements are used to store and retrieve data from a structure

• A period (.) separates the structure variable name and the member name

• Examples:CurrentContact.FirstName = "Joe"

CurrentContact.LastName = "Smith"

CurrentContact.Telephone = "775-555-1288"

CurrentContact.DateAdded = #3/22/2006#

The With Statement (Introduction)

• The With statement supplies a shorthand way of referencing structure and class members

• A With block begins with the With statement and ends with the End With statement

The With Statement (Example)

With CurrentContact

.FirstName = "Joe"

.LastName = "Smith"

.Telephone = "775-555-1288"

.DateAdded = #3/22/2006#

End With

Recommended