You are on page 1of 2

Department of Computer Science CSI311 File Systems and Data Management 2012/2013 Lab 1 Merging Sequential Files

Introduction Two sequential files, having the same component type and sequenced on the same key field, can be combined to produce a single file sequenced on the common key. For example, a file containing the following fictitious employee information: 10001 Mercy Baitseng 10002 Kedirileng Metshameko 10103 Kenny Smart 10120 Bontle Muzila And another file containing the following information 10011 John Hamilton 10053 Mogorosi Modise 10055 Gaone Mabase 10133 David Calvin Both sequenced on Employee ID, when merged, will produce the following: 10001 Mercy Baitseng 10002 Kedirileng Metshameko 10011 John Hamilton 10053 Mogorosi Modise 10055 Gaone Mabase 10120 Bontle Muzila 10133 David Calvin

Page 1/2

An algorithm for accomplishing this could be written as (assuming both files are ordered): 1. Read one record from each input file 2. Move the input record with the smaller key to the output record, and replace the input record. 3. WHILE output record key <> Terminator DO BEGIN 3.1 Write output record to output file 3.2 Move the input record with smaller key to the output record, and replace the input record END Reading records from a specified file requires one to keep checking for the end of file. At the end of the file the procedure should set the record read to Terminator whose value should be greater than the largest record to be processed.

Exercises: 1. Create the two files given as text files and write the merger program in C++ or Java 2. Write the necessary programs to read and store the two input text files as binary files, and modify your merger program to merge them into a third file.

Page 2/2

You might also like