Opening Files [ HP Pascal/iX Programmer's Guide ] MPE/iX 5.0 Documentation
HP Pascal/iX Programmer's Guide
Opening Files
Except when using input and output files, your program must open files
before it can use them. A call to a predefined file-opening procedure
has the following syntax and parameters.
Syntax
{reset }
{rewrite} (logical_file [, physical_file [, open_options]])
{append }
{open }
Parameters
reset, rewrite, The names of the predefined file-opening procedures.
append, open See Table 3-2 for more information on them.
logical_file The name of the logical file to be opened.
physical_file A string or PAC expression whose value is the name of
the physical file to be opened. The syntax of the
file name is system-dependent (see Appendix A for
the MPE/iX operating system or Appendix B for the
HP-UX operating system).
open_options A string or PAC expression whose value is a list of
file attributes. The syntax of the list is
system-dependent (see Appendix A for the MPE/iX
operating system or Appendix B for the HP-UX
operating system).
Example 1
reset(logfile);
rewrite(logfile2,physfile2);
append(lfile1,pfile1,'SHARED'); {HP-UX operating system ignores 'SHARED'}
open(lfile1);
If you specify physical_file, the system associates it with logical_file.
If logical_file was previously associated with another physical file, the
system closes the other physical file with its data intact and opens a
new physical file.
Example 2
PROGRAM prog;
VAR
datafile : FILE OF integer;
BEGIN
open (datafile, 'file1'); {Logical file datafile is associated with
physical file file1.}
open (datafile, 'file2'); {Physical file file1 is closed.
Logical file datafile is associated with
physical file file2.}
END.
If logical_file is not a program parameter, and physical_file is not
specified, logical_file remains associated with its previously associated
physical file. If logical_file was not previously associated with a
physical file, the system associates logical_file with a temporary,
nameless physical file.
Example 3
PROGRAM prog; {Logical files logfile1 and logfile2 are not
program parameters}
VAR
logfile1,
logfile2 : text;
BEGIN
reset(logfile1,'file1'); {Logical file logfile1 is associated with
physical file file1.}
rewrite(logfile1); {No physical file is specified, so logical file
logfile1 remains associated with physical file file1.}
rewrite(logfile2); {No physical file is specified, and logical file
logfile2 is not associated with a physical file,
so logfile2 is associated with a temporary,
nameless physical file.}
END.
If logical_file is a program parameter, and physical_file is not
specified, the system opens the physical file that has the same name as
logical_file (with the lowercase letters upshifted--see Appendix B
for HP-UX implications). If no such physical file exists, the result
depends on whether either append or rewrite opened the logical file. If
so, the system creates the physical file. If not, it is an error.
Example 4
For this example, assume that the physical file file1 exists, but the
physical file file2 does not.
PROGRAM prog (file1,file2); {Logical files file1 and file2
are program parameters.}
VAR
file1,
file2 : text;
BEGIN
rewrite(file1); {Logical file file1 is associated with the
physical file file1.}
rewrite(file2); {Logical file file2 is associated with a
physical file file2. }
END.
A temporary, nameless physical file cannot be saved. It becomes
inaccessible when the main program or routine that declared logical_file
terminates, or when you associate logical_file with a new physical file.
Your program does not need to open the standard textfiles input and
output. When they are program parameters, the operating system opens
them with reset and rewrite, respectively.
The standard textfiles input and output are bound to specific system
files. For the MPE/iX operating system, see Appendix A ; for the
HP-UX operating system, see Appendix B .
Table 3-2 summarizes the characteristics of the four predefined
file-opening procedures.
Table 3-2. Characteristics of File-Opening Procedures
-----------------------------------------------------------------------------------------------------
| | | | | |
| Procedure | Reset | Rewrite | Append | Open |
| | | | | |
-----------------------------------------------------------------------------------------------------
| | | |
| Type of file | Any | Any except |
| That it Can | | textfile |
| Open | | |
| | | |
-----------------------------------------------------------------------------------------------------
| | | | |
| State in | Read-only | Write-only | Read-Write |
| Which it | | | |
| Opens File | | | |
| | | | |
-----------------------------------------------------------------------------------------------------
| | | |
| Manner in | Sequentially | Directly |
| Which file | | |
| Can Be | | |
| Accessed | | |
| | | |
-----------------------------------------------------------------------------------------------------
| | | | | |
| Purpose for | Input | Output | Output at | Input |
| Which it | | over old | end of old | and |
| Opens File | | contents | contents | output |
| | | | | |
-----------------------------------------------------------------------------------------------------
| | | | | |
| Where it Puts | First | Before | After | Before |
| Current | component | first | last | first |
| Position | | component | component | component |
| Index * | | | | |
| | | | | |
-----------------------------------------------------------------------------------------------------
| | | | |
| Value of eof | False | True | False |
| for File * | | | |
| | | | |
-----------------------------------------------------------------------------------------------------
| | | | |
| Erases Old | No | Yes | No |
| File Contents | | | |
| | | | |
-----------------------------------------------------------------------------------------------------
| | | |
| File Buffer | Contains | Undefined |
| Variables * | value of | |
| | first | |
| | component | |
| | | |
-----------------------------------------------------------------------------------------------------
* For a nonempty file. For an empty file, every file-opening
procedure puts the current position index before the [nonexistent]
first component, eof returns true, and the file buffer variable is
undefined.
MPE/iX 5.0 Documentation