Presentation is loading. Please wait.

Presentation is loading. Please wait.

Methods of accessing a File

Similar presentations


Presentation on theme: "Methods of accessing a File"— Presentation transcript:

1 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.

2 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.

3 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

4 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

5 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


Download ppt "Methods of accessing a File"

Similar presentations


Ads by Google