Transcript Ch9 notes

Chapter 9
Random Access Files
Outline and Objectives
Fixed-Length Strings
Records
Random Access Files
Chapter 9 - Visual Basic Schneider
Fixed - Length String
Each variable in the program is assigned
to a data type.
Fixed - Length String variable: Stores
character strings of fixed length
To declare a string of length n use the
following statement:
Dim var As String * n
Note: n is a positive integer
Chapter 9 - Visual Basic Schneider
Example
Private Sub cmdGo_Click()
Dim city As String * 9
‘ Illustrate fixed-length strings
picOutput.Cls
picOutput.Print "123456789"
city = "San Francisco"
picOutput.Print city
city = "Detroit"
picOutput.Print city; "MI"
picOutput.Print Len(city)
End Sub
Chapter 9 - Visual Basic Schneider
Comparing Strings:
Care must be taken to compare two fixedlength strings of different length.
Use the function RTrim to remove the
right hand spaces from a variable - length
or a fixed - length string.
Example: RTrim(city)
Chapter 9 - Visual Basic Schneider
Records:
Record is a user defined data type
A record is a collection of fields
Each field in the record has a name, type
and length which is the number of spaces
allocated to it.
Chapter 9 - Visual Basic Schneider
Syntax of a Record Definition
‘In General Declarations
Private Type recordType
field-name1 As fieldType1
field-name2 As fieldType2
………
End Type
Chapter 9 - Visual Basic Schneider
Record Declaration
Dim record-variable As recordType
Chapter 9 - Visual Basic Schneider
Example of Record Definition
Private Type studentRecord
name As String *30
idNumber As String *11
gpa As Single
numOfCredit As Integer
End Type
Chapter 9 - Visual Basic Schneider
To create a record of a
specific type
Dim student1 As studentRecord
Chapter 9 - Visual Basic Schneider
Storing Data in a Record
student1.name = “John Smith”
student1.idNumber = “456-78-9012”
student1.gpa = 3.5
student1.numOfCredits = 124
Chapter 9 - Visual Basic Schneider
Methods of accessing a
File
Sequential access
Data is accessed in the same order in which it is
physically stored in the file.
Random access
Records are accessed directly from the file in
random order.
Chapter 9 - Visual Basic Schneider
Random Access Files
A Random Access file is usually made up
of a collection of records.
Records are accessed directly from the file
in random order.
Usually is done by using record’s number
in the file.
Chapter 9 - Visual Basic Schneider
Opening a Random Access File
One statement is suffices for creating,
appending, writing and reading a random
access file.
Open “filespec” For Random As #n Len=Len(recVar)
Chapter 9 - Visual Basic Schneider
Example of entering a record into a file:
Private Sub cmdAddCollege_Click()
‘ Write a record into the file COLLEGES.TXT
Dim college As collegeData
college.nom = txtCollege.Text
college.state = txtState.Text
college.yrFounded = Val(txtYear.Text)
recordNum = recordNum + 1
Put #1, recordNum, college
Entering a
txtCollege.Text = ""
txtState.Text = ""
txtYear.Text = ""
txtCollege.SetFocus
End Sub
Chapter 9 - Visual Basic Schneider
record into a file
Example of reading a record from a file:
Private Sub DisplayFile()
Dim recordNum As Integer
‘ Access the random-access file COLLEGES.TXT
Dim college As collegeData
Open "COLLEGES.TXT" For Random As #1 Len = Len(college)
picOutput.Print "College", , "State", "Year founded"
For recordNum = 1 To 3
Get #1, recordNum, college
To Read a data from a file
picOutput.Print college.nom, college.state, college.yrFounded
Next recordNum
Close #1
End Sub
Chapter 9 - Visual Basic Schneider
Summary of Using Random
Access Files:
Do not need to close between placing records
into them or reading from them
Do not need to input the records in any specific
order
To find out the most recent record number in a
file #n, then use the function Loc(n)
Each record should have the same length, which
is any number from 1 to 32767
Chapter 9 - Visual Basic Schneider