File Merge
To merge two files is to combine them into a single file.

Two files can only be merged together if they have the same structure - the fields are defined the same in each file.

The new merged file contains all the records of both the merged files.

The two files must be sorted in the same way. The resulting merged file will also be sorted in the same way.

 

Merging files (in words) :
  1. A new file is created
  2. A record is read from each of the two files.
  3. The record which comes first (in sorting order) is placed on the new file and is replaced by reading a new record from the original file.
  4. The process [3] is repeated until both files are empty.

File 1 and File 2 are merged together to create a new merged file which contains all records from File 1 and File 2.

File 1, File 2 and the new file will ...

  • have the same data structure
  • be sorted in the same way

Files may be saved on media other than magnetic tape.


Example :

File 1 :

ID No Sold today
305 3
309 1
316 2
317 1

File 2:

The two files to be merged are sorted in order of ID.
ID No Sold today
301 5
310 1
319 7

 

Merged file :
ID No Sold today
301 5
305 3
309 1
310 1
316 2
317 1
319 7

 

The merged file is also sorted in order of ID.
Any number of files may be merged together if they are properly sorted and have the same data structure.

 

Exercise : File Merge
To two files involves combining them together into a new file.

The two files to be merged must be and the new file will be sorted in the same order.

 

(Green indicates success)