Accessing Files Programmer's Guide (32650-90885)

222 AppendixA
Pascal/XL Program Examples
Program Example A-4
(see procedure read_user_label).
3. In a loop, read (FREADDIR) even numbered records from the disk file. Before writing
(FWRITE) the records to disk, prefetch the next record (FREADSEEK). Do this till EOF of
the disk file is reached (see procedure read_from_datafile).
4. Close (FCLOSE) both files (see procedure close_files).
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-4. Random Access
program Read_Example (input,output);
{************************************************************************}
{ DECLARATION PART }
{************************************************************************}
const
CCG = 0; { condition code warning }
CCL = 1; { condition code error }
CCE = 2; { condition code successful }
type
file_name = packed array [1..9] of char;
buffertype = packed array [1..80] of char;
var
datafile_name: file_name;
listfile_name: file_name;
buffer : buffertype;
message : buffertype;
datafile : shortint;
listfile : shortint;
record_num : integer;
function fopen:shortint; intrinsic; { open files }
procedure freadlabel; intrinsic; { read user-defined label }
procedure freaddir; intrinsic; { random access read file }
procedure fwrite; intrinsic; { sequential write to $STDLIST }
procedure fclose; intrinsic; { close files }
procedure freadseek; intrinsic; { prefetch selected record }
procedure printfileinfo; intrinsic; { used in error-handler }
procedure quit; intrinsic; { used in error-handler }
procedure error_handler (filenum, quitnum: shortint);
{************************************************************************}
{ procedure error_handler is a standard file system error handling }
{ procedure invoked after an unsuccessful file system intrinsic call. }
{ A file information display is printed to $STDLIST, then program aborts.}
{************************************************************************}