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

28
Chapter 10 Sequential Files and Structures

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

Page 1: Chapter 10 Sequential Files and Structures. Class 10: Sequential Files Work with different types of sequential files Read sequential files based on the.

Chapter 10

Sequential Files and Structures

Page 2: Chapter 10 Sequential Files and Structures. Class 10: Sequential Files Work with different types of sequential files Read sequential files based on the.

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

Page 3: Chapter 10 Sequential Files and Structures. Class 10: Sequential Files Work with different types of sequential files Read sequential files based on the.

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

Page 4: Chapter 10 Sequential Files and Structures. Class 10: Sequential Files Work with different types of sequential files Read sequential files based on the.

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

Page 5: Chapter 10 Sequential Files and Structures. Class 10: Sequential Files Work with different types of sequential files Read sequential files based on the.

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

Page 6: Chapter 10 Sequential Files and Structures. Class 10: Sequential Files Work with different types of sequential files Read sequential files based on the.

Figure 10-6:Delimited Sequential File

Page 7: Chapter 10 Sequential Files and Structures. Class 10: Sequential Files Work with different types of sequential files Read sequential files based on the.

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

Page 8: Chapter 10 Sequential Files and Structures. Class 10: Sequential Files Work with different types of sequential files Read sequential files based on the.

Figure 10-7:Fixed-field File

Page 9: Chapter 10 Sequential Files and Structures. Class 10: Sequential Files Work with different types of sequential files Read sequential files based on the.

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

Page 10: Chapter 10 Sequential Files and Structures. Class 10: Sequential Files Work with different types of sequential files Read sequential files based on the.

The StreamReader Constructor (Example)

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

Dim CurrentReader As _

System.IO.StreamReader = _

New System.IO.StreamReader( _

"C:\Demo.txt")

Page 11: Chapter 10 Sequential Files and Structures. Class 10: Sequential Files Work with different types of sequential files Read sequential files based on the.

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

Page 12: Chapter 10 Sequential Files and Structures. Class 10: Sequential Files Work with different types of sequential files Read sequential files based on the.

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

Page 13: Chapter 10 Sequential Files and Structures. Class 10: Sequential Files Work with different types of sequential files Read sequential files based on the.

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

Page 14: Chapter 10 Sequential Files and Structures. Class 10: Sequential Files Work with different types of sequential files Read sequential files based on the.

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

Page 15: Chapter 10 Sequential Files and Structures. Class 10: Sequential Files Work with different types of sequential files Read sequential files based on the.

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

Page 16: Chapter 10 Sequential Files and Structures. Class 10: Sequential Files Work with different types of sequential files Read sequential files based on the.

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

Page 17: Chapter 10 Sequential Files and Structures. Class 10: Sequential Files Work with different types of sequential files Read sequential files based on the.

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

Page 18: Chapter 10 Sequential Files and Structures. Class 10: Sequential Files Work with different types of sequential files Read sequential files based on the.

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

Page 19: Chapter 10 Sequential Files and Structures. Class 10: Sequential Files Work with different types of sequential files Read sequential files based on the.

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

Page 20: Chapter 10 Sequential Files and Structures. Class 10: Sequential Files Work with different types of sequential files Read sequential files based on the.

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

Page 21: Chapter 10 Sequential Files and Structures. Class 10: Sequential Files Work with different types of sequential files Read sequential files based on the.

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

Page 22: Chapter 10 Sequential Files and Structures. Class 10: Sequential Files Work with different types of sequential files Read sequential files based on the.

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

Page 23: Chapter 10 Sequential Files and Structures. Class 10: Sequential Files Work with different types of sequential files Read sequential files based on the.

Structures (Syntax)

[Public | Friend] Structure name

variableDeclarations [procedureDeclarations]End Structure

Page 24: Chapter 10 Sequential Files and Structures. Class 10: Sequential Files Work with different types of sequential files Read sequential files based on the.

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

Page 25: Chapter 10 Sequential Files and Structures. Class 10: Sequential Files Work with different types of sequential files Read sequential files based on the.

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

Page 26: Chapter 10 Sequential Files and Structures. Class 10: Sequential Files Work with different types of sequential files Read sequential files based on the.

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#

Page 27: Chapter 10 Sequential Files and Structures. Class 10: Sequential Files Work with different types of sequential files Read sequential files based on the.

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

Page 28: Chapter 10 Sequential Files and Structures. Class 10: Sequential Files Work with different types of sequential files Read sequential files based on the.

The With Statement (Example)

With CurrentContact

.FirstName = "Joe"

.LastName = "Smith"

.Telephone = "775-555-1288"

.DateAdded = #3/22/2006#

End With