Previous Topic Next topic Print topic


Record File Attributes

The following attributes are appropriate for use with record files. Some may be used in either the DECLARE statement for the file constant or in the OPEN statement, others only in the OPEN statement. Some attributes, if present, imply the presence of certain others.

Attribute Permitted in Specification      Implied
RECORD DECLARE or OPEN  
INPUT DECLARE or OPEN  
OUTPUT DECLARE or OPEN  
UPDATE DECLARE or OPEN RECORD
SEQUENTIAL DECLARE or OPEN RECORD
KEYED DECLARE or OPEN RECORD
DIRECT DECLARE or OPEN RECORD KEYED
ENVIRONMENT      DECLARE  
TITLE OPEN  

If neither INPUT, OUTPUT, nor UPDATE is specified, INPUT is assumed. If neither SEQUENTIAL nor DIRECT is specified, SEQUENTIAL is assumed. If neither STREAM nor RECORD is specified, the default is STREAM. (See the section Stream I/O.) The following sets of attributes are mutually exclusive:

If attributes are specified both in the DECLARE statement and the OPEN statement for a file, the attributes used are the combination of those in the two statements. The combination may include only one of each mutually exclusive set.

The TITLE option is used to associate a file as declared in the PL/I program with a file as known to the operating system. In the following examples of OPEN statements, the quoted text in the TITLE options specifies the system filename to be used in I/O operations with this opening of the PL/I file indicated by the FILE option. If the TITLE option is not specified in an OPEN statement, the declared name of the file constant is used as the default title.

Examples:

OPEN FILE(F) RECORD OUTPUT TITLE('thedata');

OPEN FILE(F) RECORD INPUT; /* The default title is 'F' */

If a READ, WRITE, REWRITE, or DELETE statement is executed on a file that has not been opened, the file is implicitly opened as a RECORD file using the declared name of the file constant as the title. The attributes assigned to the file implicitly opened depend on the I/O statement used, as indicated below:

Record I/O Statement      Assigned Attributes
READ RECORD INPUT
WRITE RECORD OUTPUT
REWRITE RECORD UPDATE
DELETE RECORD UPDATE

Record input and output operations are restricted to files opened with certain attributes. These restrictions are shown in the following table:

File Attributes      Permitted I/O Operations
INPUT READ
OUTPUT WRITE
UPDATE READ, WRITE, REWRITE, DELETE

If a file opened for INPUT or UPDATE does not exist, an error is signaled.

If a file opened for OUTPUT does not exist, a file is created. If the file has been opened as DIRECT or KEYED SEQUENTIAL, a keyed file is created; otherwise, a non-keyed file is created.

If a file opened for OUTPUT already exists, it is deleted and a new file is created.

However, if the -APPEND option is specified in opening the file, an existing file can be opened for OUTPUT. You can add records to an existing record file by using the -APPEND option in the TITLE option of the OPEN statement. Thus, if the file you wish to append to is named "logfile," open it with the following TITLE option:

TITLE ('logfile -APPEND') 

Any subsequent WRITE statements to this file simply append their output to the end of the file.

A REWRITE statement replaces an existing record in an UPDATE file with a new record. A DELETE statement basically removes an existing record from an UPDATE file. A WRITE to an existing record and a READ from a nonexistent record are not allowed. A WRITE to a record past EOF will append the record to the end of the file. A DELETE or REWRITE of a nonexistent record signals a KEY condition. (For further information, see the description of KEY in the section ON in the chapter Statements.)

Record I/O statements copy the storage of a variable to or from a record in a file (except in the case of READ operations using locate mode I/O, see below).

No conversion is performed and no check is made to ensure that the data being read is of the proper type to store into the variable. The variables used in INTO or FROM options cannot be unaligned bit strings or structures consisting entirely of unaligned bit strings, because such variables normally share a portion of their storage with other members of the same array or structure. Also, an expression cannot be used in a FROM or INTO option.

Note:

Structures with the UNION attribute cannot be used as variables in READ, WRITE, PUT, and GET statements.

READ operations can be performed in two "modes": move mode and locate mode. A READ statement that specifies the INTO option causes the input data to be stored directly into the variable referenced by the INTO option. This is called a "move mode" READ. A READ statement that specifies the SET option instead of the INTO option causes the input data to be stored in an unnamed storage buffer and causes the address of this buffer to be assigned to the pointer variable referenced by the SET option. This is called a "locate mode" READ.

A complete discussion of record I/O statements is found in the chapter Statements.

Examples:

READ FILE(CUSTOMERS) KEY(CUST_ID) INTO(CUST_RECORD); 
PUT SKIP LIST(CUST_RECORD.NAME);
READ FILE(CUSTOMERS) KEY(CUST_ID) SET(CPTR); 
PUT SKIP LIST(CPTR->BASED_RECORD.NAME);
Previous Topic Next topic Print topic