HPlogo Accessing Files Programmer's Guide > Chapter 8 Writing to a File

Sequential Access and Random Access

MPE documents

Complete PDF
Table of Contents
Index

E0300 Edition 6 ♥
E0692 Edition 5

Two of the most frequently used methods of transferring data to a file from your program are sequential access and random access.

When you use sequential access to write data to a file, you write data to the record currently pointed to by the record pointer. You use the FWRITE intrinsic to write data sequentially to a disk file or device file. When you open a file with any form of write access (except Append) specified in the access type option of HPFOPEN/FOPEN, the file is opened with the record pointer set to the first record in the file. 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. Both disk files and device files can be accessed with the FWRITE intrinsic.

When you use random access to write data to a disk file, you write data to any record in the file by specifying where you want the file system to set the record pointer prior to the write 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 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            }

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 EOF in old_disk_file}

     if ccode = ccl then
        handle_file_error  (old_disk_file);      {error check on 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}
  end;                                        {end procedure         }

The operation is performed in a loop. Before each write operation, record_num is decremented. The loop ends when the FREAD intrinsic encounters an EOF in old_disk_file (indicating the end of the file). For more information about FWRITEDIR intrinsic parameters, refer to the MPE/iX Intrinsics Reference Manual. For more information about the FREAD intrinsic, refer to chapter 9, "Reading from a File". In appendix A, "HP Pascal/iX Program Examples", example A-3 uses a similar routine to copy records using the random access method of data transfer to write date from one file to another.




Chapter 8 Writing to a File


Writing to $STDLIST