Title: 10 Files
110 Files
2Learning Objectives
- Explain what a file does and why a file is
useful. - Give the general code for dealing with random
access files (remembering fixed length strings).
3A File
- Stores data permanently
- In all programs so far, any stored data has been
lost when the program closes, and has to be
entered again when the program next runs.
4Random (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.
5Restrictions of Random Access Files
- You can only read or write whole records.
- You cannot read or write individual elements of
records. - Records must be of equal length so that VB knows
where each record is. - As you may already know (1.44 Files) Number
variables already have fixed lengths. - 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).
6Filename
- 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.
7Decorating Business Example Project
- Open the Decorating Business Example Project.
- Open the code for the first form frmQuoteFinder
(code view).
8Globally in frmQuoteFinder
- Declare a public variable to hold the path and
name of the customers file. - Note it must be public as it needs to be
accessible on other forms. - Public CustomersFile CurDir()
- \Customers.txt
- Now write a similar line underneath for the
FinishesFile
9Declaring Fixed Length Strings
- To do this use
- ltVBFixedString(30)gt Dim As String
- before declaring the string.
Length in bytes (30 is suitable for most cases)
Variable name / identifier
10Fixed Length Strings
- In the structure for customer records add the
part in bold below - Structure CustomerQuotesRecord
- ltVBFixedString(30)gt Dim CustomerName As String
- Dim QuotationDate As Date
- Dim Quote As Decimal
- End Structure
- Do the same for the Finishes record structure.
11Open a random access file
- FileOpen(1, FileName, OpenMode.Random)
- Open using random access.
- Reference number of the file used by VB to
identify it. - You only need to use a higher number if you wish
to open more than one file at a time.
12Writing to a random access file
Record to write
Record Number
13Closing a random access file
14Storing records in the file
- As while as storing finishes and their prices in
the array we also have to now store them in the
file.
15frmEnterAFinish - butEnterFinish code
- At the end of the procedure write
- FileOpen (1, frmQuoteFinder.FinishesFile,
OpenMode.Random) - FilePut (1, frmQuoteFinder. FinishesArray(frmQuote
Finder.NumberOfFinishes), form1.NumberOfFinishes) - FileClose(1)
- Now write similar lines to store customer details
in the CustomersFile (in the frmCreateQuote
butCreateQuote code).
16Reading all records from a file and inserting
them into an array
- Do While Not EOF(1)
- NumberOfRecords NumberOfRecords 1
- FileGet(1, Array(NumberOfRecords))
- Trim (remove spaces) any fixed length string
fields - Loop
EOF End Of File
Makes rows in file match rows in array and counts
number of records on file.
17Retrieving records from the file
- We now have to retrieve records from the file
when the program is run. - The best place to do this is in the
frmQuoteFinders load event as this is the first
form to appear and the load event is the first
event which happens automatically.
18Form1 load event
- FileOpen (1, FinishesFile, OpenMode.Random)
- Do While Not EOF(1)
- NumberOfFinishes NumberOfFinishes 1
- FileGet(1, FinishesArray(NumberOfFinishes))
- FinishesArray(NumberOfFinishes).Finish _
Trim(FinishesArray(NumberOfFinishes). Finish) - Loop
- FileClose(1)
19Retrieving records from the CustomersFile
- Now write similar lines to retrieve customer
details in the CustomersFile.
20Plenary
- 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.
21Plenary
- What is the general code for dealing with random
access files (remembering fixed length strings)? - ltVBFixedString(30)gt Dim As String
- FileOpen(1, FileName, OpenMode.Random)
- FilePut(1, , )
- FileGet(1, )
- FileClose(1)
- Use Do While EOF(1) Loop if you wish to read
all records.
22Homework
- 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 on a
separate form. - Extension
- A user should be able to edit or delete a record.