Accessing Files Programmer's Guide (32650-90885)

Appendix A 217
Pascal/XL Program Examples
Program Example A-3
}
close_tape_file (tape_file); { STEP 5 }
close_disk_file (disk_file); { STEP 6 }
end. { main }
Program Example A-3
This HP Pascal/XL program illustrates how you can use a sequential method of reading
records from an old disk file and use a random access method of writing the records in an
inverted order to a new user-labeled disk file, where record 1 of the first file is written to
location n of the second file, record 2 is written to location
n
-1, and so on.
Program Algorithm
The task specified above is accomplished by following the steps described below. Also
indicated are the intrinsics used to accomplish file access tasks and the name of the
procedure where the task is accomplished:
1. Open (HPFOPEN) a permanent disk file and a new user-labeled disk file (see procedure
open_disk_file).
2. Write (FWRITELABEL) a user-defined label to the new file (see procedure
write_user_label).
3. Get EOF (FGETINFO) of old file and assign that value to new file's record pointer; in a
loop, sequentially read (FREAD) records from old file and write (FWRITEDIR) them to a
location in the new file specified by the record pointer, then decrement the new file's
record pointer (see procedure copy_oldfile_to_newfile). Continue the loop till the
old file's EOF is reached.
4. Close (FCLOSE) the old file as deleted from the system, and close the new file as a
temporary file (see procedure close_disk_file).
If a file system intrinsic returns an unsuccessful condition code, procedure
handle_file_error is called to print file information (PRINTFILEINFO) and then abort
(QUIT) the program.
Source code listing
Example A-3. Random Access
$standard_level 'hp3000'$
$lines 100$
$code_offsets on$
$tables on$
$list_code on$
program write_read (input,output);
{***********************************************************************}