BACS 287 File-Based Programming. BACS 287 Data Hierarchy Database - Collection of files,...
-
Upload
marsha-gilmore -
Category
Documents
-
view
229 -
download
0
description
Transcript of BACS 287 File-Based Programming. BACS 287 Data Hierarchy Database - Collection of files,...
BACS 287
BACS 287
File-Based Programming
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
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
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
. . . .5050505050
You must read throughthe 1st four records toget to 5th
1st 2nd 3rd 4th 5th
BACS 287
StreamReaders & StreamWriters
Sequential files can be processed as a “stream” of data
StreamReaders are VB objects that read data from a stream
StreamWriters are VB objects that write data to a stream
Neither can be used before the file is opened
BACS 287
Example StreamReader Statements
Creating and reading from StreamReaderDim stVar as IO.StreamReaderstVar = IO.File.OpenText(filespec)Dim StrVar = srVar.ReadLinesrVar.EndOfStreamSrVar.Close()
BACS 287
StreamReaders Use
strVar = srVar.ReadLine reads the line pointed to, assigns the line to the string variable strVar, and moves the pointer to the next line of the file.
The value of srVar.EndOfStream will be True after the entire file has been read.
The statement srVar.Close() terminates communication with the file.
BACS 287
StreamReader Use
If sr is a variable of type StreamReader, an entire text file can be read with a loop of the formDo while not sr.EndOfStream strVar = srVar.ReadLine . .Loop
BACS 287
Example StreamWriter Statements
Creating and writing to StreamWriterDim swVar as IO.StreamWriterswVar = IO.File.CreateText(filespec)swVar.WriteLIne(strData)swVar.Close()
BACS 287
StreamWriter Use
swVar.WriteLine(info) initally places the information into the first line of the file.
Subsequent statements of that form place information into lines at the end of the file.
The statement swVar.Close() terminates communication with the file.
BACS 287
Text File Modes
OpenText – open for input CreateText – open for output AppendText – open for append A file should not be opened in two different
modes at the same time.
BACS 287
Example StreamWriter Statements
Adding to existing file via StreamWriterIf (IO.File.Exists(filespec))…Dim swVar as IO.StreamWriterswVar = IO.File.AppendText(filespec)swVar.WriteLIne(strData)swVar.Close()
BACS 287
StreamWriter Use
1. Execute the statement Dim swVar As IO.StreamWriter = _ IO.File.AppendText(filespec) where filespec identifies the file. 2. Add lines of data to the end of the file with
the WriteLine method.3. After all the data have been written into the
file, close the file with swVar.Close().Note: If the file does not exist, the AppendText method will create it.
BACS 287
Avoiding Errors
Attempting to open a non-existent file for input brings up a message box titled:
FileNotFoundException There is a method to determine if a file exists
before attempting to open it: IO.File.Exists(filespec)
will return True if the file exists.
BACS 287
Test for Existence of File
Dim sr As IO.StreamReaderIf IO.File.Exists(filespec) Then sr = IO.File.OpenText(filespec)Else message = "Either no file has yet been " message &= "created or the file named" message &= filespec & " is not found." MessageBox.Show(message, "File Not Found")End If
BACS 287
Deleting Data from a Sequential File
An individual item of a sequential file cannot be changed or deleted directly.
A new file must be created by reading each item from the original file and recording it, with the single item changed or deleted, into the new file.
The old file is then erased, and the new file renamed with the name of the original file.
BACS 287
Delete and Move Methods
Delete method: IO.File.Delete(filespec) Move method (to change the filespec of a
file): IO.File.Move(oldfilespec, newfilespec)
Note: The IO.File.Delete and IO.File.Move methods cannot be used with open files.
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
. . . .5050505050
5th recordstarts atthe 201stbyte
1st 2nd 3rd 4th 5th
BACS 287
VB Type Statement
TYPE structurenamefieldname1 AS datatypefieldname2 AS datatype...
END TYPE
TYPE employeeName as string * 30Address as string * 50Salary as integer
END TYPE
BACS 287
VB File-Based ExamplePublic Function Open_Data_File()Rem*******************************************************************Rem Routine to open a new data file on the user disk. lngRecLengthRem 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
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
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 finishedClose #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, wrkRecordPut #1, Position, wrkRecord
BACS 287
Random File Manipulation
RAM
CPU
Disk Drive(contains the
file)Disk Controller
BUS