Chapter 9 Random Access Files Outline and Objectives

Chapter 9 Random Access Files

Outline and Objectives z. Fixed-Length Strings z. Records z. Random Access Files Chapter 9 - Visual Basic Schneider

Fixed - Length String z. Each variable in the program is assigned to a data type. z. Fixed - Length String variable: Stores character strings of fixed length z. 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 cmd. Go_Click() Dim city As String * 9 ‘ Illustrate fixed-length strings pic. Output. Cls pic. Output. Print "123456789" city = "San Francisco" pic. Output. Print city = "Detroit" pic. Output. Print city; "MI" pic. Output. Print Len(city) End Sub Chapter 9 - Visual Basic Schneider

Comparing Strings: z. Care must be taken to compare two fixedlength strings of different length. z. Use the function RTrim to remove the right hand spaces from a variable - length or a fixed - length string. z. Example: RTrim(city) Chapter 9 - Visual Basic Schneider

Records: z. Record is a user defined data type z. A record is a collection of fields z. 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 record. Type field-name 1 As field. Type 1 field-name 2 As field. Type 2 ……… End Type Chapter 9 - Visual Basic Schneider

Record Declaration Dim record-variable As record. Type Chapter 9 - Visual Basic Schneider

Example of Record Definition Private Type student. Record name As String *30 id. Number As String *11 gpa As Single num. Of. Credit As Integer End Type Chapter 9 - Visual Basic Schneider

To create a record of a specific type z. Dim student 1 As student. Record Chapter 9 - Visual Basic Schneider

Storing Data in a Record student 1. name = “John Smith” student 1. id. Number = “ 456 -78 -9012” student 1. gpa = 3. 5 student 1. num. Of. Credits = 124 Chapter 9 - Visual Basic Schneider

Methods of accessing a File z. Sequential access x. Data is accessed in the same order in which it is physically stored in the file. z. Random access x. Records are accessed directly from the file in random order. Chapter 9 - Visual Basic Schneider

Random Access Files z. A Random Access file is usually made up of a collection of records. z. Records are accessed directly from the file in random order. z. Usually is done by using record’s number in the file. Chapter 9 - Visual Basic Schneider

Opening a Random Access File z. One statement is suffices for creating, appending, writing and reading a random access file. Open “filespec” For Random As #n Len=Len(rec. Var) Chapter 9 - Visual Basic Schneider

Example of entering a record into a file: Private Sub cmd. Add. College_Click() ‘ Write a record into the file COLLEGES. TXT Dim college As college. Data college. nom = txt. College. Text college. state = txt. State. Text college. yr. Founded = Val(txt. Year. Text) record. Num = record. Num + 1 Put #1, record. Num, college Entering txt. College. Text = "" txt. State. Text = "" txt. Year. Text = "" txt. College. Set. Focus End Sub a record into a file Chapter 9 - Visual Basic Schneider

Example of reading a record from a file: Private Sub Display. File() Dim record. Num As Integer ‘ Access the random-access file COLLEGES. TXT Dim college As college. Data Open "COLLEGES. TXT" For Random As #1 Len = Len(college) pic. Output. Print "College", , "State", "Year founded" For record. Num = 1 To 3 Get #1, record. Num, college To Read a data from a file pic. Output. Print college. nom, college. state, college. yr. Founded Next record. Num Close #1 End Sub Chapter 9 - Visual Basic Schneider

Summary of Using Random Access Files: z Do not need to close between placing records into them or reading from them z Do not need to input the records in any specific order z To find out the most recent record number in a file #n, then use the function Loc(n) z Each record should have the same length, which is any number from 1 to 32767 Chapter 9 - Visual Basic Schneider
- Slides: 17