The following program merges the personnel files shown at the beginning of the
previous example. The input files are already sorted by employee number.
In this example, the files are merged by employee number. The record size is
determined by the input files. The status parameter is checked
after the calls to HPMERGEINIT and HPMERGEEND.
Example 2-2. MERGEFILE Program
program MERGEFILE (input,output);
{This program reads the files, TEMPEMP and PERMEMP, merges}
{them by employee number, and outputs to the file, ALLEMP }
var
tempFileNum: INTEGER;
permFileNum: INTEGER;
outFileNum : INTEGER;
status : INTEGER;
procedure HPFOPEN ; intrinsic;
procedure HPMERGEINIT; intrinsic;
procedure HPMERGEERRORMESS; intrinsic;
procedure HPMERGEEND; intrinsic;
procedure FCLOSE; intrinsic;
procedure OPEN_FILES;
const
designator = 2;
domain = 3;
access = 11;
record_size = 19;
var
tempFile : packed array [1..10] of CHAR;
permFile : packed array [1..10] of CHAR;
outFile : packed array [1..10] of CHAR;
permanent : INTEGER;
new : INTEGER;
write : INTEGER;
size : INTEGER;
tempFile := '%TEMPEMP%';
permanent := 1;
HPFOPEN (tempFileNum, status, designator, tempFile, domain,
permanent);
permFile := '%PERMEMP%';
HPFOPEN (permFileNum, status, designator, permFile, domain,
permanent);
new := 4;
write := 1;
size := 80;
outFile := '%ALLEMP%';
HPFOPEN (oOutFileNum, status, designator, outFile, domain, new,
access, write, record_size, size);
end;
procedure DO_MERGE;
var
inputfiles : array [1..3] of INTEGER;
outputfile : array [1..2] of INTEGER;
keysonly : INTEGER;
numKeys : INTEGER;
keys : array [1..4] of INTEGER;
altseq : packed array [1..2] of CHAR;
message : packed array [1..80] of CHAR;
length : INTEGER;
begin
inputfiles [1] := tempFileNum;
inputfiles [2] := permFileNum;
inputfiles [3] := 0;
outputfile [1] := outFileNum; {from HPFOPEN}
outputfile [2] := 0;
keysonly := 0; {output record format same as input record}
numKeys := 1; {one key}
keys[1] := 41; {key begins}
keys[2] := 20; {key length}
keys[3] := 0; {byte data}
keys[4] := 0; {ascending order}
altseq[1] := CHR(255); {data = ASCII; sequence = ASCII}
altseq[2] := CHR(255); {256 characters in ASCII}
HPMERGEINIT (status, inputfiles,, outputfile,, keysonly,
numKeys, keys, altseq,,,,,);
if status <> 0 then {if error in HPMERGEINIT}
begin {get message and print it to screen}
message := ' ';
HPMERGEERRORMESS (status, message, length);
writeln (Message);
end;
HPMERGEEND (status, statistics);
if status <> 0 then {if error in HPMERGEEND}
begin {get message and print it to screen}
message := ' ';
HPMERGEERRORMESS (status, message, length);
writeln (message);
end;
end;
procedure CLOSE_FILES;
var
disposition : SHORTINT;
securityCode : SHORTINT;
begin
disposition := 0;
securityCode := 0;
FCLOSE (tempFileNum, disposition, securityCode);
FCLOSE (permFileNum, disposition, securityCode);
disposition := 1;
FCLOSE (outFileNum, disposition, securityCode);
end;
begin {main}
OPEN_FILES;
DO_MERGE;
CLOSE_FILES;
end.
When this program is executed, the output is written to ALLEMP.
To view ALLEMP: