Chapter 10 Sequential Files and Structures Class 10

  • Slides: 28
Download presentation
Chapter 10 Sequential Files and Structures

Chapter 10 Sequential Files and Structures

Class 10: Sequential Files • Work with different types of sequential files • Read

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

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

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-bycharacter or all at once

The Format of Delimited Files • All delimited files have a well-defined structure and

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

Figure 10 -6: Delimited Sequential File

The Format of Fixed-field Files • Fixed-field files contain textual characters • A specific

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

Figure 10 -7: Fixed-field File

Opening and Closing Sequential Files with the Stream. Reader • The Stream. Reader and

Opening and Closing Sequential Files with the Stream. Reader • The Stream. Reader and Stream. Writer 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 Stream. Reader Constructor (Example) • Open a file named "C: Demo. txt" Dim

The Stream. Reader Constructor (Example) • Open a file named "C: Demo. txt" Dim Current. Reader As _ System. IO. Stream. Reader = _ New System. IO. Stream. Reader( _ "C: Demo. txt")

Closing a Sequential File • The Close method closes a sequential file • Always

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: Current. Reader. Close()

Reading the Entire Contents of a File (Example) • Read the file named "C:

Reading the Entire Contents of a File (Example) • Read the file named "C: Demo. txt" Dim Current. String As String Dim Current. Reader As New _ Stream. Reader("C: Demo. txt") Current. String = _ Current. Reader. Read. To. End() Current. Reader. Close()

Reading a Sequential File Character by Character • It's possible to read a sequential

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 Current. Char As Integer Dim

Reading a Sequential File Character by Character (Example) Dim Current. Char As Integer Dim Current. Reader As New _ Stream. Reader("C: Demo. txt") Current. Char = Current. Reader. Read() Do Until Current. Char = – 1 ' Statements to process the character. Current. Char = Current. Reader. Read() Loop Current. Reader. Close()

Reading a Sequential File One Record at a Time • Delimited files are processed

Reading a Sequential File One Record at a Time • Delimited files are processed using the following steps: – Call the Read. Line 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 Current. Reader As

Reading a Sequential File as a List of Records (Example) Dim Current. Reader As New _ System. IO. Stream. Reader("C: Demo. txt") Dim Current. Record As String Current. Record = Current. Reader. Read. Line() Do Until Current. Record = Nothing ' Statements to process the current record. Current. Record = Current. Reader. Read. Line() Loop Current. Reader. Close()

Writing a Sequential File • The Stream. Writer class of the System. IO namespace

Writing a Sequential File • The Stream. Writer class of the System. IO namespace writes a sequential file • The constructor accepts one argument – the file to write • Example: Dim Current. Writer As New _ System. IO. Stream. Writer("C: Demo. txt") ' Statements to write the file. Current. Writer. Close()

The Stream. Writer Class (Members) • The New. Line property contains the character(s) that

The Stream. Writer Class (Members) • The New. Line 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 Write. Line method writes data terminated by the character(s) stored in the New. Line property

Writing a Freeform File • A freeform file can be written all at once

Writing a Freeform File • A freeform file can be written all at once as follows: Dim String. Data As String = "Freeform text" Dim Current. Writer As New _ System. IO. Stream. Writer("C: Demo. txt") Current. Writer. Write(String. Data) Current. Writer. Close()

Writing a Delimited File • The steps to write delimited file are as follows:

Writing a Delimited File • The steps to write delimited file are as follows: – Create an instance of the Stream. Writer class – Using a For loop, call the Write. Line method to write each record – Call the Close method when writing is complete

Writing a Delimited File (Example) • Write an array of Integers Public Shared Sub

Writing a Delimited File (Example) • Write an array of Integers Public Shared Sub Write. Integer. List( _ By. Ref arg. Array() As Integer, _ By. Val arg. File As String) Dim Current. Stream As New Stream. Writer(arg. File) Dim Current. Index As Integer For Current. Index = 0 To arg. Array. Get. Upper. Bound(0) Current. Stream. Write(arg. Array(Current. Index)) If Current. Index <> _ arg. Array. Get. Upper. Bound(0) Then Current. Stream. Write(", ") End If Next Current. Stream. Close() End Function

Introduction to Structures • A structure is used to store related data items, and

Introduction 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 variable. Declarations [procedure. Declarations] End Structure

Structures (Syntax) [Public | Friend] Structure name variable. Declarations [procedure. Declarations] End Structure

Structures (Syntax, continued) • The Structure and End Structure keywords mark the beginning and

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 • variable. Declarations contains the structure members • procedure. Declarations contains procedures appearing in the structure

Structures (Example) • Declare a structure named Contact with four members Public Structure Contact

Structures (Example) • Declare a structure named Contact with four members Public Structure Contact Public First. Name As String Public Last. Name As String Public Telephone. Number As String Public Date. Added As Date. Time End Structure

Storing and Retrieving Data From a Structure • Assignment statements are used to store

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: Current. Contact. First. Name = "Joe" Current. Contact. Last. Name = "Smith" Current. Contact. Telephone = "775 -5551288" Current. Contact. Date. Added = #3/22/2006#

The With Statement (Introduction) • The With statement supplies a shorthand way of referencing

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 Current. Contact. First. Name = "Joe". Last. Name =

The With Statement (Example) With Current. Contact. First. Name = "Joe". Last. Name = "Smith". Telephone = "775 -555 -1288". Date. Added = #3/22/2006# End With