Download presentation
Presentation is loading. Please wait.
1
Random Access Files / Direct Access Files
23/02/2019
2
Learning Objectives Explain concepts of files, records, fields.
Explain simple linear file structure – with a variety of fields of fixed length. Explain the function of key fields. Explain the difference between text and random access files. Give the general code for dealing with random access files (remembering fixed length strings). 23/02/2019
3
Data Data stored in computers is normally connected in some way.
For example, some data about 20 students has a connection because it all refers to the same set of people. Each person will have the same information stored about them, for instance their name, address, telephone number, exam grades… 23/02/2019 3 3
4
Fields & Records Each column is a field. Each row is a record. 45278
Account Number Surname Forename Balance 45278 Smith Sally €10.00 1208 Jones John €20.00 3217 Cain Shazad €100.00 4310 White Peter €250.00 23/02/2019 4 4
5
File A large quantity of data that has an identity.
e.g. Data about a set of students. 23/02/2019 5 5
6
Key Field Some fields may contain the same items of data in more than one record. e.g. there may be two people with the same name or balance. It is important that the computer can identify individual records, and it can only do this if it can be sure that one of the fields will always contain different data in all the records. The key field is unique and is used to identify the record. In our example the key field would be the account number. 23/02/2019 6 6
7
Random (Direct) Access Files
Store only records. You can read and write to any record position within the file. You can edit records within the file. Can be opened for read and write access at the same time. 23/02/2019
8
Restrictions of Random Access Files
23/02/2019
9
Can only read or write whole records
You cannot read or write individual elements of records. 23/02/2019
10
Fixed Length Records should be of a fixed length. Advantages:
The file can be stored and searched faster. It also makes it possible to estimate the file size and so choose a suitable storage medium with enough space. Disadvantages: Field sizes must be chosen before use so space may be wasted or may not be enough. 23/02/2019
11
Fixed Length However, string variables use 1 byte per character (so are as long as the text they hold). String variables in a random access file record need to be of a fixed length (this will remove characters if the string is longer than the fixed length or add spaces to extend it to reach the fixed length). 23/02/2019
12
Declaring Fixed Length Strings
To do this use <VBFixedString(30)> Dim … As String before declaring the string. Length in bytes (30 is suitable for most cases) Variable name / identifier 23/02/2019
13
Filename The full path and name of the file.
It is best to use a variable to hold all this: Dim FileName As String = CurDir() & "\....txt" Name of file.txt Finds the path of the current program, this will store the file in the same folder. This will be in the program folder – a folder with the same name – bin – Debug. It is best to do this otherwise every time you move the program you would have to change the path accordingly.
14
Open a random access file
FileOpen(1, FileName, OpenMode.Random) 23/02/2019
15
Writing to a random access file
FilePut(1, … , …) Record to write Record Number 23/02/2019
16
Reading from a random access file
FileGet(1, …) Record to read 23/02/2019
17
Reading from a file If you wish to read all records at once then FileGet should be contained within a loop (to ensure that you don’t go read past the end of a file and force an error). Do While Not EOF(1) FileGet(1, …) Trim any string fields to remove spaces. Loop EOF = End Of File This will loop through all records.
18
Closing a random access file
FileClose(1) 23/02/2019
19
7.2a Student Test Marks - File
Extend the program 6 Student Test Marks to use a file. 23/02/2019
20
7.2a Student Test Marks - File
Add <VBFixedString(20)> to the name field of the Student Record Structure. So it now becomes: Structure StudentStructure 'Strings in records need to be of a fixed length so that VB knows where each record begins and ends. '(This means spaces will be added or letters removed). <VBFixedString(20)> Dim Name As String Dim Mark As Integer End Structure New 23/02/2019
21
7.2a Student Test Marks - File
'The following variable will store the name and path of the Students File. Dim StudentsFile As String = CurDir() & "\StudentsFile.txt" 23/02/2019
22
7.2a Student Test Marks - File
Students(NumberOfStudents).Name = Console.ReadLine Students(NumberOfStudents).Mark = Console.ReadLine 'Open the Students File. FileOpen(1, StudentsFile, OpenMode.Random) 'Store the new student record in the next record of the Students File. FilePut(1, Students(NumberOfStudents), NumberOfStudents) FileClose(1) 'Close the StudentsFile. New 23/02/2019
23
7.2 Student Test Marks - File
After variable declarations but before the first Do Loop. 'This code basically retrieves previously stored student names and marks from the Student file and places them in the Students array. ' 'Open the Students File. FileOpen(1, StudentsFile, OpenMode.Random) 23/02/2019 Continued on next slide.
24
7.2a Student Test Marks - File
'Loop through to the end of the CustomerQuotesFile. Do While Not EOF(1) 'Increment the Number of Students by 1. 'This variable will serve as a counter to move through the Students ‘Array and will by the end of loop 'hold the number of students currently on file. NumberOfStudents = NumberOfStudents + 1 'Take each student on file and place it in the next element of the Students array. FileGet(1, Students(NumberOfStudents)) 'Remove extra spaces at the end of each name inserted by VB due ‘to the name field being a fixed string. Students(NumberOfStudents).Name = Trim(Students(NumberOfStudents).Name) Loop 23/02/2019 Continued on next slide.
25
7.2a Student Test Marks - File
FileClose(1) 'Close the Student File. 23/02/2019
26
7.2a Student Test Marks - File
Run the program and test it. Extend the program to also reset the file. ‘Clear the file. FileOpen(1, FileName, OpenMode.Output) FileClose(1) ‘Close the file. 23/02/2019
27
Pseudocode for File Handling
OPENFILE <filename> FOR READ/WRITE / APPEND Open file Understand the difference between various file modes. READFILE <filename>, <string> Read a line of text from the file. WRITEFILE <filename>, <string> Write a line of text to the file. CLOSEFILE <filename> Close file. EOF (<filename>) Function to test for the end of the file.
28
Extension “Estate Agency File” Program 7.2b
Extend the “Estate Agency” Program - 6 Records so that the queries are also held in an file (and an array while the program is running) so that all queries stored will be retrievable even after the program is closed and opened again. Extension: Adapt the program so that there is no more than 1 entry per customer. If there is already a record for that customer it should be replaced by the new one. Adapt the program to store a new query when the file already contains 10 queries. Only attempt this last point if you really want a challenge as it is a very complex idea. I suggest this is only done if you have lots of time to spare. 23/02/2019
29
Extension “Student Records” Program 7.2c
Write a program to store student records in a random access file. Each record should hold the number of warnings, merits and a comment field. A user should be able to view the records. Extension: A user should be able to edit or delete a record. 23/02/2019
30
Plenary Explain what a file does and why a file is useful.
Store data permanently. So data does need to be re-entered every time the program starts. 23/02/2019
31
Plenary What is the general code for dealing with random access files (remembering fixed length strings)? <VBFixedString(30)> Dim … As String FileOpen(1, FileName, OpenMode.Random) FileGet(1, …) FileClose(1) Use Do While EOF(1) … Loop if you wish to read all records. 23/02/2019
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.