30.07.2013 Views

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

794 Files and Streams Chapter 17<br />

17.8 Creating a Random-Access File<br />

Consider the following problem statement for a credit-processing application:<br />

Create a transaction-processing program capable of s<strong>to</strong>ring a maximum of 100 fixed-length<br />

records for a company that can have a maximum of 100 cus<strong>to</strong>mers. Each record consists of<br />

an account number (that acts as the record key), a last name, a first name and a balance.<br />

The program can update an account, create an account and delete an account.<br />

The next several sections introduce the techniques necessary <strong>to</strong> create this credit-processing<br />

program. We now discuss the program used <strong>to</strong> create the random-access file that the<br />

programs of Fig. 17.16 and Fig. 17.17 and the transaction-processing application use <strong>to</strong><br />

manipulate data. Class FrmCreateRandomAccessFile (Fig. 17.15) creates a random-access<br />

file.<br />

1 ' Fig. 17.15: CreateRandomAccessFile.vb<br />

2 ' Creating a random file.<br />

3<br />

4 ' <strong>Visual</strong> <strong>Basic</strong> namespaces<br />

5 Imports System.IO<br />

6 Imports System.Windows.Forms<br />

7<br />

8 ' Deitel namespaces<br />

9 Imports BankLibrary<br />

10<br />

11 Public Class CCreateRandomAccessFile<br />

12<br />

13 ' number of records <strong>to</strong> write <strong>to</strong> disk<br />

14 Private Const NUMBER_OF_RECORDS As Integer = 100<br />

15<br />

16 ' start application<br />

17 Shared Sub Main()<br />

18<br />

19 ' create random file, then save <strong>to</strong> disk<br />

20 Dim file As CCreateRandomAccessFile = _<br />

21 New CCreateRandomAccessFile()<br />

22<br />

23 file.SaveFile()<br />

24 End Sub ' Main<br />

25<br />

26 ' write records <strong>to</strong> disk<br />

27 Private Sub SaveFile()<br />

28<br />

29 ' record for writing <strong>to</strong> disk<br />

30 Dim blankRecord As CRandomAccessRecord = _<br />

31 New CRandomAccessRecord()<br />

32<br />

33 ' stream through which serializable data is written <strong>to</strong> file<br />

34 Dim fileOutput As FileStream<br />

35<br />

36 ' stream for writing bytes <strong>to</strong> file<br />

37 Dim binaryOutput As BinaryWriter<br />

Fig. 17.15 FrmCreateRandomAccessFile class create files for random-access<br />

file-processing applications (part 1 of 3).

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!