Accessing Files Programmer's Guide (32650-90885)

Chapter 8 121
Writing to a File
Sequential Access and Random Access
If an error is encountered by either FWRITE or FREAD, the condition code CCL is returned to
the program, thus invoking the procedure handle_file_error. For more information
about FWRITE parameters, refer to the MPE/iX Intrinsics Reference Manual. For more
information about using the FREAD intrinsic, refer to chapter 9, "Reading from a File". For
more information about opening a file, refer to chapter 5, "Opening a File". In appendix A,
"HP Pascal/iX Program Examples," example A-1 uses a similar procedure to copy records
from a tape file to a disk file.
Writing to a disk file using random access
Example 8-2 is an HP Pascal/iX code segment that reads records sequentially from
old_disk_file and writes them into new_disk_file. Assume that both files have been
opened already with calls to HPFOPEN/FOPEN. The end-of-file (EOF) using the FWRITEDIR of
old_disk_file is determined with the FGETINFO intrinsic and assigned to the variable
record_num.
Example 8-2. Writing to a Disk File Using Random Access.
procedure copy_from_old_file_to_new_file;
var
record_num : integer;
buffer : packed array [256] of char;
end_of_file : boolean;
read_length : integer;
length : shortint;
begin
end_of_file := false; {initialize exit condition }
record_num := 0; {initialize record pointer }
length := 128 {also means 256 bytes }
FGETINFO (old disk_file,,,,,,,,,,rec); {locate the EOF in old_disk_file}
if ccode = ccl then
handle_file_error (old_disk_file); {error check on intrinsic call}
repeat {Copy the records in the reverse}
{orders from old disk file }
{to the new disk file }
read_length := FREAD (old_disk_file, buffer, length);
if ccode = ccl then
handle_file_error (old_disk_file)
else
if ccode = ccg then {check for exit condition}
end_of_file := true
else begin
rec := rec - 1 {decrement record pointer}
FWRITEDIR(new_disk_file, buffer, read_length, record_num);
if ccode <> cce then
handle_file_error (new_discfile); {error check }
end
until end_of_file {exit loop if exit condition true}