Download presentation
1
Chapter 9 Random Access Files
2
Outline and Objectives
Fixed-Length Strings Records Random Access Files Chapter 9 - Visual Basic Schneider
3
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
4
Chapter 9 - Visual Basic Schneider
Example Private Sub cmdGo_Click() Dim city As String * 9 ‘ Illustrate fixed-length strings picOutput.Cls picOutput.Print " " city = "San Francisco" picOutput.Print city city = "Detroit" picOutput.Print city; "MI" picOutput.Print Len(city) End Sub Chapter 9 - Visual Basic Schneider
5
Chapter 9 - Visual Basic Schneider
Comparing Strings: Care must be taken to compare two fixed-length 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
6
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
7
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
8
Chapter 9 - Visual Basic Schneider
Record Declaration Dim record-variable As recordType Chapter 9 - Visual Basic Schneider
9
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
10
To create a record of a specific type
Dim student1 As studentRecord Chapter 9 - Visual Basic Schneider
11
Storing Data in a Record
student1.name = “John Smith” student1.idNumber = “ ” student1.gpa = 3.5 student1.numOfCredits = 124 Chapter 9 - Visual Basic Schneider
12
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. Qbasic allows you to create two different types of files: sequential and random accesss. Chapter 9 - Visual Basic Schneider
13
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. Access method: the way in which the computer transmits data between secondary storage and the primary storage. Chapter 9 - Visual Basic Schneider
14
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
15
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 txtCollege.Text = "" txtState.Text = "" txtYear.Text = "" txtCollege.SetFocus End Sub Entering a record into a file Chapter 9 - Visual Basic Schneider
16
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 picOutput.Print college.nom, college.state, college.yrFounded Next recordNum Close #1 End Sub To Read a data from a file Chapter 9 - Visual Basic Schneider
17
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.