7 2 Files Random Access Files Direct Access

  • Slides: 31
Download presentation
7. 2 Files Random Access Files / Direct Access Files 12/14/2021 1

7. 2 Files Random Access Files / Direct Access Files 12/14/2021 1

Learning Objectives Explain concepts of files, records, fields. Explain simple linear file structure –

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). 12/14/2021 2

Data stored in computers is normally connected in some way. For example, some 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… 12/14/2021 3

Fields & Records Each column is a field. Each row is a record. Account

Fields & Records Each column is a field. Each row is a record. Account Number Surname Forename Balance 45278 1208 3217 4310 Smith Jones Cain White Sally John Shazad Peter € 10. 00 € 20. 00 € 100. 00 € 250. 00 12/14/2021 4

File A large quantity of data that has an identity. n e. g. Data

File A large quantity of data that has an identity. n e. g. Data about a set of students. 12/14/2021 5

Key Field Some fields may contain the same items of data in more than

Key Field Some fields may contain the same items of data in more than one record. n 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. n In our example the key field would be the account number. 12/14/2021 6

Random (Direct) Access Files Store only records. You can read and write to any

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. 12/14/2021 7

Restrictions of Random Access Files 12/14/2021 8

Restrictions of Random Access Files 12/14/2021 8

Can only read or write whole records You cannot read or write individual elements

Can only read or write whole records You cannot read or write individual elements of records. 12/14/2021 9

Fixed Length Records should be of a fixed length. Advantages: n n The file

Fixed Length Records should be of a fixed length. Advantages: n n 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: n Field sizes must be chosen before use so space may be wasted or may not be enough. 12/14/2021 10

Fixed Length n However, string variables use 1 byte per character (so are as

Fixed Length n 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). 12/14/2021 11

Declaring Fixed Length Strings n To do this use <VBFixed. String(30)> Dim … As

Declaring Fixed Length Strings n To do this use <VBFixed. String(30)> Dim … As String n before declaring the string. Length in bytes (30 is suitable for most cases) 12/14/2021 Variable name / identifier 12

Filename The full path and name of the file. It is best to use

Filename The full path and name of the file. It is best to use a variable to hold all this: Dim File. Name As String = Cur. Dir() & ". . 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. Name of file. txt

Open a random access file File. Open(1, File. Name, Open. Mode. Random) 12/14/2021 14

Open a random access file File. Open(1, File. Name, Open. Mode. Random) 12/14/2021 14

Writing to a random access file File. Put(1, … , …) Record to write

Writing to a random access file File. Put(1, … , …) Record to write 12/14/2021 Record Number 15

Reading from a random access file File. Get(1, …, …) Where to place the

Reading from a random access file File. Get(1, …, …) Where to place the record from the file 12/14/2021 Record to read 16

Reading from a file If you wish to read all records at once then

Reading from a file If you wish to read all records at once then File. Get should be contained within a loop (to ensure that you don’t go read past the end of a file and force an error) and so the record to read parameter is no longer needed. Do While Not EOF(1) EOF = End Of File n File. Get(1, typically the array(row index) to place each file record) n Trim any string fields to remove spaces. Loop This will loop through all records.

Closing a random access file File. Close(1) 12/14/2021 18

Closing a random access file File. Close(1) 12/14/2021 18

7. 2 a Student Test Marks - File Extend the program 6 Student Test

7. 2 a Student Test Marks - File Extend the program 6 Student Test Marks to use a file. 12/14/2021 19

7. 2 a Student Test Marks - File n Add <VBFixed. String(20)> to the

7. 2 a Student Test Marks - File n Add <VBFixed. String(20)> to the name field of the Student Record Structure. New So it now becomes: n Structure Student. Structure '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). <VBFixed. String(20)> Dim Name As String n 12/14/2021 Dim Mark As Integer End Structure 20

7. 2 a Student Test Marks - File n n 'The following variable will

7. 2 a Student Test Marks - File n n 'The following variable will store the name and path of the Students File. Dim Students. File As String = Cur. Dir() & "Students. File. txt" 12/14/2021 21

7. 2 a Student Test Marks - File Students(Number. Of. Students). Name = Console.

7. 2 a Student Test Marks - File Students(Number. Of. Students). Name = Console. Read. Line Students(Number. Of. Students). Mark = Console. Read. Line New 'Open the Students File. Open(1, Students. File, Open. Mode. Random) 'Store the new student record in the next record of the Students File. Put(1, Students(Number. Of. Students), Number. Of. Students) File. Close(1) 'Close the Students. File. 12/14/2021 22

7. 2 Student Test Marks - File After variable declarations but before the first

7. 2 Student Test Marks - File After variable declarations but before the first Do Loop. n n '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. Open(1, Students. File, Open. Mode. Random) 12/14/2021 Continued on next slide. 23

7. 2 a Student Test Marks - File 'Loop through to the end of

7. 2 a Student Test Marks - File 'Loop through to the end of the Customer. Quotes. File. Do While Not EOF(1) n n n n n '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. Number. Of. Students = Number. Of. Students + 1 'Take each student on file and place it in the next element of the Students array. File. Get(1, Students(Number. Of. Students)) 'Remove extra spaces at the end of each name inserted by VB due ‘to the name field being a fixed string. Students(Number. Of. Students). Name = Trim(Students(Number. Of. Students). Name) Loop 12/14/2021 Continued on next slide. 24

7. 2 a Student Test Marks - File n File. Close(1) 'Close the Student

7. 2 a Student Test Marks - File n File. Close(1) 'Close the Student File. 12/14/2021 25

7. 2 a Student Test Marks - File Run the program and test it.

7. 2 a Student Test Marks - File Run the program and test it. Extend the program to also reset the file. ‘Clear the file. File. Open(1, File. Name, Open. Mode. Output) File. Close(1) ‘Close the file. 12/14/2021 26

Pseudocode for File Handling OPENFILE <filename> FOR READ/WRITE / APPEND n Open file Understand

Pseudocode for File Handling OPENFILE <filename> FOR READ/WRITE / APPEND n Open file Understand the difference between various file modes. READFILE <filename>, <string> n Read a line of text from the file. WRITEFILE <filename>, <string> n Write a line of text to the file. CLOSEFILE <filename> n Close file. EOF (<filename>) n Function to test for the end of the file.

Extension “Estate Agency File” Program 7. 2 b Extend the “Estate Agency” Program -

Extension “Estate Agency File” Program 7. 2 b 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. n 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. n 12/14/2021 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 28 time to spare.

Extension “Student Records” Program 7. 2 c Write a program to store student records

Extension “Student Records” Program 7. 2 c 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: n A user should be able to edit or delete a record. 12/14/2021 29

Plenary Explain what a file does and why a file is useful. n n

Plenary Explain what a file does and why a file is useful. n n Store data permanently. So data does need to be re-entered every time the program starts. 12/14/2021 30

Plenary What is the general code for dealing with random access files (remembering fixed

Plenary What is the general code for dealing with random access files (remembering fixed length strings)? n n <VBFixed. String(30)> Dim … As String File. Open(1, File. Name, Open. Mode. Random) File. Get(1, …) File. Close(1) Use Do While EOF(1) … Loop if you wish to read all records. 12/14/2021 31