Jump to content United States-English
HP.com Home Products and Services Support and Drivers Solutions How to Buy
» Contact HP
More options
HP.com home
HP Pascal/HP-UX Programmer's Guide > Chapter 3 Input/Output

Opening Files

» 

Technical documentation

Complete book in PDF
» Feedback
Content starts here

 » Table of Contents

 » Glossary

 » Index

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

[{resetrewriteappendopen} (logical_file [, physical_file [, open_options]])]

Parameters

reset, rewrite, append, open

The names of the predefined file-opening procedures. See Table 3-2 “Characteristics of File-Opening Procedures” 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 “MPE/iX Dependencies ” for the MPE/iX operating system or Appendix B “HP-UX Dependencies ” 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 “MPE/iX Dependencies ” for the MPE/iX operating system or Appendix B “HP-UX Dependencies ” 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 “HP-UX Dependencies ” 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 “MPE/iX Dependencies ”; for the HP-UX operating system, see Appendix B “HP-UX Dependencies ”.

Table 3-2 “Characteristics of File-Opening Procedures” 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 That it Can Open

Any

Any except textfile

State in Which it Opens File

Read-only

Write-only

Read-Write

Manner in Which file Can Be Accessed

Sequentially

Directly

Purpose for Which it Opens File

Input

Output over old contents

Output at end of old contents

Input and output

Where it Puts Current Position Index *

First component

Before first component

After last component

Before first component

Value of eof for File *

False

True

False

Erases Old File Contents

No

Yes

No

File Buffer Variables *

Contains value of first component

Undefined

 

Title not available (Opening Files )

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.

Printable version
Privacy statement Using this site means you accept its terms Feedback to webmaster
© Hewlett-Packard Development Company, L.P.