HPlogo Accessing Files Programmer's Guide > Chapter 5 Opening a File

Opening a Disk File

MPE documents

Complete PDF
Table of Contents
Index

E0300 Edition 6 ♥
E0692 Edition 5

Disk files are files residing on volumes (disk packs). Disk files are immediately accessible by the system and potentially shareable by several jobs or sessions at the same time. The following examples show how you can use the HPFOPEN intrinsic to open a disk file:
  • "Opening a new disk file" shows an example of an HPFOPEN call that creates a new disk file (see example 5-1).

  • "Opening a permanent disk file" shows an example of an HPFOPEN call that opens a permanent disk file that is to be shared among multiple concurrent accessors (see example 5-2).

Opening a new disk file


Example 5-1 is an HP Pascal/iX code segment containing an HPFOPEN intrinsic call that opens a new disk file to be used with a text editor. The text editor accesses only standard ASCII text files with fixed-length records, each record 80 bytes in size.

Knowing this, you can specify the appropriate HPFOPEN options, and allow others (specifically, domain option, record format option, and file type option) to default to the desired characteristics. Note that the HPFOPEN final disposition option is specified to indicate that the file is to be saved as a temporary file at close time.

Example 5-1. Opening a New Disk File

  procedure open_new_disk_file;

  const
     formal_designator_option = 2;     {defines HPFOPEN itemnum 2  }
     record_size_option       =19;     {defines HPFOPEN itemnum 19 }
     final_disposition_option =50;     {defines HPFOPEN itemnum 50 }
     ASCII_binary_option      =53;     {defines HPFOPEN itemnum 53 }

  type
     pac80  = packed array [1..80] of char;

  var
     file_num    : integer;  {required HPFOPEN filenum parameter   }
     status      : integer;  {returns info if error/warning occurs }

     file_name   : pac80;             {declares HPFOPEN itemnum 2  }
     line_len    : integer;           {declares HPFOPEN itemnum 19 }
     save_perm   : integer;           {declares HPFOPEN itemnum 50 }
     ascii       : integer;           {declares HPFOPEN itemnum 53 }
                               
  begin
     file_num    :=0;
     status      :=0;
     file_name   :='&myfile/lock.mygroup&'; (filereference format}
     line_len    :=80;           {maximum record/line length     }
     save_temp   :=2;            {make temp file at close        }
     ascii       :=1;            {label indicates ASCII code     }

     HPFOPEN (file_num, status,
       formal_designator_option,file_name,{formal designator option}
       record_size_option, line_len,      {record size option      }
       final_disp_option, save_temp,      {final disposition option}
       ASCII_binary_option, ascii         {ASCII/binary option     }
       );

     if status <> 0 then handle_file_error (file_num, status);
  end;

If the HPFOPEN call is successful, a positive integer value is returned in file_num, and status returns a value of zero. The new disk file is now open and can be accessed with system intrinsics. If an error or warning condition is encountered by HPFOPEN, status returns a nonzero value, thus invoking the error-handling procedure handle_file_error.

In appendix A, "HP Pascal/iX Program Examples," Example A-1 uses a similar procedure to open a new disk file. For more information about HPFOPEN parameters, refer to the MPE/iX Intrinsics Reference Manual.

Opening a permanent disk file


Example 5-2 is an HP Pascal/iX code segment containing an HPFOPEN intrinsic call that opens a permanent disk file that is to be shared among multiple concurrent accessors. Note the use of the dynamic locking option to enable the use of file-locking intrinsics (FLOCK and FUNLOCK) with this file. The file is opened update access to allow opening the file with Read/Write access without affecting the current EOF. Thus, current data in the file is retained.

Example 5-2. Opening a Permanent Disk File

  procedure open_permanent_disk_file;

  const
     formal_designator_option = 2;    {defines HPFOPEN itemnum 2  }
     domain_option            = 3;    {defines HPFOPEN itemnum 3  }
     access_type_option       =11;    {defines HPFOPEN itemnum 11 }
     dynamic_locking_option   =12;    {defines HPFOPEN itemnum 12 }
     exclusive_option         =13;    {defines HPFOPEN itemnum 13 }
     ASCII_binary_option      =53;    {defines HPFOPEN itemnum 53 }

  type
     pac80  = packed array [1..80] of char;

  var
     file_num    : integer;  {required HPFOPEN filenum parameter  }
     status      : integer;  {returns info if error/warning occurs}

     file_name   : pac80;            {declares HPFOPEN itemnum 2  }
     permanent   : integer;          {declares HPFOPEN itemnum 3  }
     update      : integer;          {declares HPFOPEN itemnum 11 }
     lockable    : integer;          {declares HPFOPEN itemnum 12 }
     shared      : integer;          {declares HPFOPEN itemnum 13 }
     ascii       : integer;          {declares HPFOPEN itemnum 53 }

  begin
     file_num    :=0;
     status      :=0;
     file_name   :='&datafile/![FINFO("datafile",33)].!hpgroup&';
     permanent   :=1;          {search in permanent file directory}
     update      :=5;             {enable update access to file   }
     lockable    :=1;             {enable dynamic locking option  }
     shared      :=3;             {allow concurrent access by all }
     ascii       :=1;             {label will indicate ASCII code }

     HPFOPEN (file_num, status,
       formal_designator_option,file_name,{formaldesignator option}
       domain_option, permanent,          {domain option}
       access_type_option, update,        {access type option}
       dynamic_locking_option, lockable,  {dynamic locking option}
       exclusive_option, shared,          {exclusive option}
       ASCII_binary_option, ascii         {ASCII/binary option}
       );

     if status <> 0 then handle_file_error (file_num, status);
  end;

The file name passed in the formaldesignator option contains MPE/iX command interpreter variables and expressions that are evaluated by HPFOPEN before the file name is parsed and evaluated. HPFOPEN substitutes ![FINFO(datafile",33)] with the lockword associated with file datafile (if the security provisions in effect enable you to obtain the file's password). The exclamation point (!) before the variable name hpgroup instructs HPFOPEN to substitute the value of the variable in place of the variable name. For more information about using command interpreter variables and expressions, refer to the Command Interpreter Access and Variables Programmer's Guide.

If the HPFOPEN call is successful, a positive integer value is returned in file_num, and status returns a value of zero. The file is now open and can be accessed with file system intrinsics. If an error or warning condition is encountered by HPFOPEN, status returns a nonzero value, thus invoking the error-handling procedure handle_file_error.

In appendix A "HP Pascal/iX Program Examples," Example A-5 uses a similar procedure to open a permanent disk file. For more information about HPFOPEN parameters, refer to the MPE/iX Intrinsics Reference Manual.




Which to Use: HPFOPEN or FOPEN


Opening a System-Defined File