9 2 Files Random Access Files Direct Access
- Slides: 26
9. 2 Files Random Access Files / Direct Access Files 2/13/2022 1
Learning Objectives Explain the difference between text and random access files. Give the general code for dealing with random access files (remembering fixed length strings). 2/13/2022 2
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. 2/13/2022 3
Restrictions of Random Access Files 2/13/2022 4
Can only read or write whole records You cannot read or write individual elements of records. 2/13/2022 5
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 2/13/2022 Field sizes must be chosen before use so space may be wasted or may not be enough. 6
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). 2/13/2022 7
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) 2/13/2022 Variable name / identifier 8
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" l Finds the path of the current program, this will store the file in the same folder. l This will be in the program folder – a folder with the same name – bin – Debug. l 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) 2/13/2022 10
Writing to a random access file File. Put(1, … , …) Record to write 2/13/2022 Record Number 11
Reading from a random access file File. Get(1, …) Record to read 2/13/2022 12
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). Do While Not EOF(1) EOF = End Of File n File. Get(1, …) n Trim any string fields to remove spaces. Loop This will loop through all records.
Closing a random access file File. Close(1) 2/13/2022 14
9. 2 Student Test Marks - File Extend the program 8 Student Test Marks to use a file. 2/13/2022 15
9. 2 Student Test Marks - File Globally: n Add <VBFixed. String(30)> 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(30)> Dim Name As String n 2/13/2022 Dim Mark As Integer End Structure 16
9. 2 Student Test Marks - File Also globally: n n 2/13/2022 'The following variable will store the name and path of the Students File. Dim Students. File As String = Cur. Dir() & "Students. File. txt" 17
9. 2 Student Test Marks - File In but. Add, after the lines which store the new student name and mark in the array: Students(Number. Of. Students). Name = txt. Name. Text Students(Number. Of. Students). Mark = txt. Mark. Text 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. 2/13/2022 18
9. 2 Student Test Marks - File In the form’s load event (double click the form): n n n n 2/13/2022 'This code is executed when this form loads - when the program is started. 'It 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) 19
In the form’s load event continued: '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 2/13/2022 20
In the form’s load event continued: File. Close(1) 'Close the Student File. 2/13/2022 21
9. 2 Student Test Marks - File Run the program and test it. 2/13/2022 22
Extension “Estate Agency File” Program 1 Extend the “Estate Agency File” Program - 8 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 2/13/2022 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 23 time to spare.
Extension “Student Records” Program 2 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 2/13/2022 A user should be able to edit or delete a record. 24
Plenary Explain what a file does and why a file is useful. n n 2/13/2022 Store data permanently. So data does need to be re-entered every time the program starts. 25
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. 2/13/2022 26
- Array advantage and disadvantage
- Serial file organization advantages and disadvantages
- Cjis meaning
- Dot powai files are binary files
- Ncic hosts restricted files and non-restricted files
- Random assignment vs random sampling
- Random assignment vs random selection
- Parallel ram
- Random access adalah
- Pengertian sdram
- Multiprocessor
- Random access control
- Alohause reviews
- Terminal access controller access-control system
- Terminal access controller access control system
- Edma memory
- Basic dma operation
- Magnetic tape is a direct access storage medium
- Direct access control
- Dma attack
- Dma direct memory access
- Direct access table
- Direct access trading
- Direct access troubleshooting
- Advantage of dma
- Direct data access
- Direct material + direct labour