BACS 287 - University of Northern Colorado

Download Report

Transcript BACS 287 - University of Northern Colorado

BACS 287
File-Based
Programming
BACS 287
File Processing Basics



Data files are used as a way to make sure that
the results of your program are available after
the program finishes.
Permanent files are stored on disks.
VB directly supports 3 basic file organizations:
–
–
–
Sequential – text files in continuous blocks
Random – for fixed length files (text or binary)
Binary – for arbitrarily structured files
BACS 287
Data Hierarchy

Database - Collection of files, relationships, integrity
information, etc



Files - All records about the entity class
Records - Collection of fields about an entity
Fields - Named group of bytes associated with a
characteristic of interest


Bytes - 8 bits, can store 1 character
Bits - Binary Digits (1,0)
BACS 287
File Processing



File processing manages data from bits up to files.
(Database requires separate database management
package).
File processing is most closely associated with the
traditional approach to programming (i.e.,
procedural).
VB .NET can support the traditional approach as well
as more modern approaches.
BACS 287
VB File-Based Example
Public Function Open_Data_File()
Rem*******************************************************************
Rem Routine to open a new data file on the user disk. lngRecLength
Rem is used to define record length to open statement.
Rem*******************************************************************
Dim lngRecLength As Long
' determine the length of recRestaurant records
lngRecLength = Len(recRestaurant)
' open a random access data file
Open "d:\287\projects\p2\p2_test\p2data.fil" For Random As #1 Len =
lngRecLength
' find the position of the last record in the data file
lngLastRecord = LOF(1) / lngRecLength
End Function
BACS 287
Sequential Data Files




Sequential data files store data in the order
that it arrives.
New data is added to the ‘back’ of the file.
If you want to find a specific record, you must
read all records from the start of the file.
This is efficient for some applications.
BACS 287
Sequential File Structure
You m us t re ad through
the 1s t four re cords to
ge t to 5th
1st
2nd
3rd
4th
50
50
50
50
5th
50
....
BACS 287
Random Data Files



Random data files are stored so you can access the
record of interest directly (thus, random files support
direct access).
Random access files have records of fixed length.
The records are defined using a special VB
command called the ‘Type Statement’.
BACS 287
Random File Structure
5th re cord
s tarts at
the 201s t
byte
1st
2nd
3rd
4th
50
50
50
50
5th
50
....
BACS 287
Random File Commands

There are a few commands that are needed
to use random files in VB.
–
–
–
–
–
Type statement
Open
Close
Get
Put
BACS 287
VB Type Statement
TYPE structurename
fieldname1 AS datatype
fieldname2 AS datatype
...
END TYPE
TYPE employee
Name as string * 30
Address as string * 50
Salary as integer
END TYPE
BACS 287
Opening/Closing Random Files

You must open a random file before you can
access its data
Open data.fil for Random as #1 Len = 50

Likewise, you close it after you are finished
Close #1

While it is open, you can add and modify records
using the manipulation commands
BACS 287
Random File Manipulation


2 commands allow you to read file contents
(get) and write contents (put).
You need 3 pieces of information for both the
Get and Put commands
–
–
–
File number
Record position
Record work area
BACS 287
Random File Manipulation
GET statement:
GET filenumber, record position, work area
PUT statement:
PUT filenumber, record position, work area
Examples:
Get #1, Position, wrkRecord
Put #1, Position, wrkRecord
BACS 287
Random File Manipulation
BUS
CPU
Disk Controller
RAM
Disk Drive
(contains the
file)
BACS 287