Accessing Files Programmer's Guide (32650-90885)

120 Chapter8
Writing to a File
Sequential Access and Random Access
operation. You use the FWRITEDIR intrinsic to write data randomly to a disk file. You must
specify in FWRITEDIR which record that you want to write to. The file system sets the
record pointer to the selected record, then transfers the data to the record from your
program's stack. When you have accomplished the write operation, the file system
automatically sets the record pointer to point to the beginning of the next record in the file.
Only disk files can be accessed with the FWRITEDIR intrinsic.
The following examples illustrate the use of file system intrinsics to perform sequential
access writes and random access writes to a disk file.
Writing to a disk file using sequential access
Example 8-1 is an HP Pascal/iX code segment that copies logical records sequentially from
an unlabeled tape file (indicated by variable tape_file_num) and uses FWRITE to write
them to a disk file (indicated by variable disk_file_num). The operation is performed in a
loop. The loop ends when the FREAD intrinsic encounters an EOF marker on the tape
(indicating the end of the file).
Example 8-1. Writing to a Disk File Using Sequential Access
procedure copy_tape_file_to_disk_file;
var
record : packed array [1..80] of char; {declare record }
end_of_file : boolean; {declare exit condition}
record_length : shortint; {size of record read }
length : shortint; {declare parameter }
control_code : 0..65535; {declare parameter }
begin
end_of_file := false; {initialize exit condition }
control_code := 0; {initialize to default }
length : -80; {size of record to be copied }
repeat {loop until exit condition }
record_length := FREAD (tape_file_num, record, length);
if ccode = ccl then {check condition code for error }
handle_file_error (tape_file, 3)
else
if ccode = ccg then {FREAD returns ccg if EOF }
end_of_file := true {exit condition encounter encountered}
else
begin
FWRITE( disk_file_num, {identity returned by HPFOPEN }
record, {read from tape_file_num }
record length, {actual size of record }
control_code {default }
);
if ccode <> cce then {check condition code for error }
handle_file_error (disk_file, 5);
end
until end_of_file;
end {end procedure }