19.12.2012 Views

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

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.

Close #1<br />

End Sub<br />

[Run, <strong>and</strong> then click on the comm<strong>and</strong> button. The following is displayed in the picture box.]<br />

Ringo 1940<br />

Barbra 1942<br />

Sylvester 1946<br />

■ MERGING SEQUENTIAL FILES<br />

In Section 6.2, we considered an algorithm for merging two arrays. This same algorithm can<br />

be applied to merging two ordered files.<br />

Suppose you have two ordered files (possibly with certain items appearing in both files),<br />

<strong>and</strong> you want to merge them into a third ordered file (without duplications). The technique<br />

for creating the third file is as follows.<br />

1. Open the two ordered files For Input <strong>and</strong> open a third file For Output.<br />

2. Try to get an item of data from each file.<br />

3. Repeat steps (a) <strong>and</strong> (b) below until an item of data is not available in one of<br />

the files.<br />

(a) If one item precedes the other, write it into the third file <strong>and</strong> try to get another<br />

item of data from its file.<br />

(b) If the two items are identical, write one into the third file <strong>and</strong> try to get<br />

another item of data from each of the two ordered files.<br />

4. At this point, an item of data has most likely been retrieved from one of the files<br />

<strong>and</strong> not yet written to the third file. In this case, write that item <strong>and</strong> all remaining<br />

items in that file to the third file.<br />

5. Close the three files.<br />

EXAMPLE 2<br />

The following program merges two ordered files of numbers into a third file.<br />

Object Property Setting<br />

frmMerge Caption Merge Two Files<br />

lblNameFirst Caption Name of first file:<br />

txtNameFirst Text (blank)<br />

lblNameSecond Caption Name of second file:<br />

txtNameSecond Text (blank)<br />

lblNameMerged Caption Name for merged file:<br />

txtNameMerged Text (blank)<br />

cmdProceed Caption Proceed to Merge<br />

picProgress<br />

Private Sub cmdProceed_Click()<br />

Dim file1 As String, file2 As String, file3 As String<br />

Dim have1data As Boolean, have2data As Boolean<br />

Dim num1 As Single, num2 As Single<br />

Dim recCount As Integer ‘Number of records in merged file<br />

‘Merge two ordered files<br />

picProgress.Cls<br />

file1 = txtNameFirst.Text<br />

file2 = txtNameSecond.Text<br />

file3 = txtNameMerged.Text<br />

Open file1 For Input As #1<br />

Using Sequential Files 225

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

Saved successfully!

Ooh no, something went wrong!