Ch 10. Updating a File [ Accessing Files Programmer's Guide ] MPE/iX 5.0 Documentation
Accessing Files Programmer's Guide
Chapter 10 Updating a File
You can use the FUPDATE intrinsic to update a logical record of a disk
file. FUPDATE affects the last logical record (or block for NOBUF files)
accessed by any intrinsic call for the file named, and writes information
from a buffer in the stack into this record. Following the update
operation, the record pointer is set to indicate the next record
position. The record number need not be supplied in the FUPDATE
intrinsic call; FUPDATE automatically updates the last record referenced
in any intrinsic call. Note that the file system assumes that the record
to be updated has just been accessed in some way.
The disk file containing the record to be updated must have been opened
with the access type option parameter of HPFOPEN/FOPEN set to update
access. In addition, the file must not contain a variable-length record
format.
Example 10-1 is an HP Pascal/iX code segment that illustrates how to use
the FUPDATE intrinsic to update records in a disk file being shared by
multiple concurrent accessors. The program segment also uses file system
locking intrinsics (FLOCK and FUNLOCK) to guarantee exclusive access to
the file while the update occurs.
The pertinent code from Example 10-1 is shown below:
:
read_length := FREAD (disk_file_num, inbuf, 128);
:
FUPDATE (disk_file_num, inbuf, 128);
:
The statements above are in a loop that follows this algorithm:
1. Read the record from the file identified by disk_file_num using
the FREAD intrinsic.
2. Write the record to STDLIST to be reviewed by user.
3. Read new data input to STDIN by user and modify record in program.
4. Using FUPDATE intrinsic, write the updated record to the location
in disk_file_num indicated by last intrinsic call (in this case,
the FREAD call shown above).
Example 10-1. Updating a Disk File
procedure update_disk_file;
{**************************************************************}
{ procedure update_disk_file updates records in the disk file }
{ with the replacement records read from $STDIN }
{**************************************************************}
var
dummy : integer;
inbuf : array [1..80] of char;
end_of_file : boolean;
read_length : integer;
begin
{Lock the file and suspend }
end_of_file := false;
FLOCK (disk_file_num, 1);
if ccode = ccl then
handle_file_error (disk_file_num, 0);
{Begin loop }
repeat
{ Read record from disk file, then write record to $STDLIST; }
{ read updated record number from $STDIN and update }
{ the disk file with the input record and unlock disk file. }
read_length := FREAD (disk_file_num, inbuf, 128);
{read in record}
if ccode = ccg then
end_of_file := true {exit condition }
else
begin
FWRITE (std_list, inbuf, -20, octal('320'));
{user reviews record}
if ccode
cce then
handle_file_error (std_list, 5);
dummy := FREAD (std_in, inbuf[20], 5);
{input updated field}
if ccode = ccl then
handle_file_error (std_list, 6);
else
if ccode = ccg then
end_of_file := true; {exit condition}
FUPDATE (disk_file_num, inbuf, 128); {update record }
if ccode
cce then
handle_file_error (disk_file_num, 7);
end
until end_of_file; {test for EOF }
FUNLOCK (disk_file_num); {final unlock of disk file}
if ccode
cce then
handle_file_error (disk_file_num, 2);
end: {end update_file }
In appendix A, "HP Pascal Program Examples," example A-5 is an HP
Pascal/iX program that uses the procedure in example 10-1 to update
records in a disk file. For more information about FUPDATE parameters,
refer to the MPE/iX Intrinsics Reference Manual (32650-90028).
NOTE A magnetic tape device is not designed to enable the
update/replacement of a single record in an existing file.
Problems occurs in maintaining the integrity of tape records if you
attempt to perform a record update operation directly to a magnetic
tape file. You should update individual records of a magnetic tape
file only when you are copying the entire contents of that file to
another file.
MPE/iX 5.0 Documentation