10 Files - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

10 Files

Description:

Explain what a file does and why a file is useful. ... Decorating Business Example Project. Open the Decorating Business Example Project' ... – PowerPoint PPT presentation

Number of Views:18
Avg rating:3.0/5.0
Slides: 23
Provided by: nei566
Category:
Tags: decorating | files

less

Transcript and Presenter's Notes

Title: 10 Files


1
10 Files
  • Random Access Files

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

3
A 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.

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

5
Restrictions 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).

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

7
Decorating Business Example Project
  • Open the Decorating Business Example Project.
  • Open the code for the first form frmQuoteFinder
    (code view).

8
Globally 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

9
Declaring 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
10
Fixed 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.

11
Open 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.

12
Writing to a random access file
  • FilePut(1, , )

Record to write
Record Number
13
Closing a random access file
  • FileClose(1)

14
Storing 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.

15
frmEnterAFinish - 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).

16
Reading 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.
17
Retrieving 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.

18
Form1 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)

19
Retrieving records from the CustomersFile
  • Now write similar lines to retrieve customer
    details in the CustomersFile.

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

21
Plenary
  • 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.

22
Homework
  • 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.
Write a Comment
User Comments (0)
About PowerShow.com