Title: Random Access File IO
1Random Access File I/O
2(No Transcript)
3Random-Access File Concepts
- Sequential files are not the most efficient means
for getting disk file - Information for instant-access applications.
- Instant-access application include the following
- Airline-reservation systems
- Banking systems
- Point-of-sale systems
- Automated-teller machine
- Instant-access applications are examples of
transaction-processing systems. - Such systems require rapid access to specific
data. - Random-access ( or direct-access) files are one
way of achieving instant access. - Sequential-access file search involves searching
through large number of records. This is not the
case with random-access files. - This class illustrates the organization of a
random-access file composed of fixed-length
records of 100 bytes each.
4Random-Access File Concepts
- Advantages over Sequential-Access Files
- Data can be inserted into a random-access file
without destroying - data in the file
- Previously-stored data can be updated or deleted
without having - to re-write the entire file.
5Bank Library Example
- The examples that follow using random-access file
I/O are - based on a banking user interface form
(BankUIForm). This form - encapsulates a base-class GUI.
- BankUIForm Source
- In order to reuse BankUIForm, compile the GUI
into a DLL by creating a project of type Windows
Control Library. Name the DLL BankLibrary.
6Creating a Random-Access File
- This project solves the following problem for a
credit-processing - application
- Create a transaction-processing program capable
of storing a maximum of 100 - fixed-length records for a company that can have
a maximum of 100 customers. - Each record consists of an account number (which
is a record key), a last name, a - first name, and a balance. The program can do
the following - Update an account
- Create an account
- Delete an account
- This program creates a random-access file
composed of random records. - Pay particular attention to what method
SaveFile() is doing. - Trace the for loop.
- This program should be created using a console
application. Use BankLibrary.dll
7(No Transcript)
8Writing Data Randomly to a Random-Access File
- A random access file has been created.
- Use class WriteRandomAccessFileForm to write data
to that file.
- Note how the Open File and Enter buttons were
added to the form - Note the Enter button code.
- Run this program as a class exercise.
- Build the program as Windows Application.
9(No Transcript)
10Writing Data Randomly to a Random-Access File
What to Look For
- Class WriteRandomAccessFileForm must determine
the location - in the FileStream at which to insert data
from the TextBoxes. - Use the Seek method of the FileStream object to
locate an exact - point in the file.
- Note where the file-position pointer is set
- (accountNumber 1) RandomAccessRecord.SIZE
- Account numbers are indexed starting at position
0. - The second argument to method Seek is a member of
the enumeration SeekOrigin - and it specifies the location at which the
method should begin seeking. - Use const SeekOrigin.Begin to seek in relation to
the beginning of the file. - After the file determines the location at which
to place the record, lines 112-118 write the
record to the file using the BinaryWriter.
11Reading Data Sequentially from a Random-Access
File
- The next task in our exploration of random-access
file I/O is - to develop a program that opens the file, reads
records from it, - and displays only the records that contain data
(i.e., those records - in which the account number is not zero.)
- Build this code using a Windows Form.
Trace the code. Understand how it works
12Reading Data Sequentially from a Random-Access
File
- The next task in our exploration of random-access
file I/O is - to develop a program that opens the file, reads
records from it, - and displays only the records that contain data
(i.e., those records - in which the account number is not zero.)
- Build this code using a Windows Form.
Trace the code. Understand how it works
13A Transaction Processing System
- This project maintains a banks account
information. - Users of this program can add new accounts,
update existing - accounts, and delete accounts.
- A class known as Transaction acts as a proxy to
handle all transaction processing. - Rather than providing transaction processing
themselves, objects in this application use an
instance of Transaction to provide the necessary
functionality. - By using a proxy, we can encapsulate
transaction-processing behavior in only one
class, enabling various other classes in our
application to reuse this behavior. - Methods of class Transaction
- OpenFile()
- GetRecord()
- AddRecord()
- The GUI for this program uses whats called a
Multiple-Document Interface (MDI). - Later in this course we will explore more details
of what an MDI is.
14A Transaction Processing System
- The MDI is organized as follows
- Class TransactionProcessorForm is the parent
window. - Children windows include
- StartDialogForm
- NewDialogForm
- UpdateDialogForm
- DeleteDialogForm
- Transaction class
- Transaction Processor Form
- Start Dialog
- Update Dialog
- New Dialog
- Delete Dialog
15A Transaction Processing System
16Homework