HP 3000 Manuals

Physical Files [ HP COBOL II/XL Programmer's Guide ] MPE/iX 5.0 Documentation


HP COBOL II/XL Programmer's Guide

Physical Files 

A physical file exists outside your program, on a device such as a tape
or disk.  In contrast, a logical file is a data structure that your
program declares.  When you associate a physical file with a logical file
and execute your program, everything that your program does to the
logical file happens to the physical file.

This section explains the following:

   *   The ASSIGN clause, which associates a logical file with a physical
       file.

   *   Temporary physical files, which exist only while your program is
       executing.

   *   The MPE BUILD command, which creates a physical file.

   *   The MPE FILE command, which associates a physical file with a
       logical file and can specify new attributes for the logical
       file.[REV BEG]

   *   How to dynamically associate a logical file with a physical file
       at run time with the USING clause.[REV END]

   *   How to access multiple physical files on a labelled tape without
       rewinding the tape.
[REV BEG]

Figure 5-1  shows how the COBOL runtime library[REV END] determines
which physical file to open when it executes an OPEN statement.

[]
Figure 5-1. Algorithm for Determining Which File to Open ASSIGN Clause The ASSIGN clause associates a logical file with a physical file. It is required for files of all types. The OPEN statement looks for a physical file named in the ASSIGN clause to associate with the logical file. It looks for these physical files in this order: 1. A file equation first. 2. A temporary physical file second. 3. A permanent physical file last.
NOTE [REV BEG] If present, the contents of the item in the USING phrase is used instead of the literal in the ASSIGN clause. See "Dynamic Files (USING phrase)" below for details.[REV END]
Temporary Physical Files If you need a physical file only while your program executes, you can use a temporary physical file. You can obtain a temporary physical file in either of these two ways: * Let COBOL create the temporary physical file automatically when you execute your program. The attributes of the temporary physical file are those that you specify with the FD and SELECT statements. (This applies only to old OUTPUT, EXTEND, and I-O files.) * Use the MPE command BUILD or FILE, specifying TEMP. BUILD Command The MPE BUILD command creates a physical file when it is executed (as opposed to specifying the attributes of a file that will be created when a program opens it). Your program can use such a physical file by opening it with the OPEN statement. For more information on the BUILD command, refer to the HP COBOL II/XL Reference Manual and the MPE XL Commands Reference Manual. FILE Command The MPE FILE command, also called a file equation, performs one or both of the following: * Associates a physical file in the MPE environment with the logical file defined by your program. * Specifies attributes for the logical file, overriding the name and other attributes that the program specifies in the OPEN statement. The FILE command can override the following attributes: * File name, including optional node name. * File size, including number of extents (new files only). * File type (CIR, MSG, or STD) (new files only). * Block size (new files only). * Whether the file is ASCII or binary (new files only). * Whether the file has carriage control (new files only). * Whether access is exclusive or shared. * Number of input/output buffers to be assigned to the file. * File disposition, which is what happens to the file after it is closed. * Device. * Output priority. * Number of copies to be printed. * Whether the magnetic tape that contains the file is labelled.
NOTE The HP COBOL II/XL compiler requires a closer match between physical file attributes and program-specified attributes when invoked through its ANSI85 entry point than it does when invoked through its ANSI74 entry point. Attempting to override a physical file attribute with a FILE equation causes permanent error 39. See Table 5-6 .
If you want to use one or more FILE commands, execute them before you execute your program. See Chapter 6 . For more information on the FILE command, refer to the MPE XL Commands Reference Manual. Figure 5-2 shows the algorithm that the run-time library uses to determine file attributes when it opens a file. For the default attributes of the :FILE command, see this command in the MPE XL Commands Reference Manual.
[]
Figure 5-2. Algorithm for Determining File Attributes Some file attributes are fixed. That is, they are established when the file is created and cannot be changed. The fixed file attributes are: * Organization. * Alternate record key. * Primary record key. * Code set. * Minimum and maximum record sizes. * Record type, fixed or variable. * Collating sequence of keys in an indexed file. * Blocking factor. The enforcement of fixed file attributes in ANSI COBOL 1974 were less stringent. If you have a fixed file attribute conflict, do one of the following: * Change your program, matching the attributes of the logical file to the fixed attributes of the physical file. * Recompile your program with the STAT74 control option. The compiler will use the less stringent checking of ANSI COBOL 1974, but you can still use its ANSI85 entry point. * Recompile your program with the ANSI74 entry point. [REV BEG] Dynamic Files (USING phrase) You can assign a logical file to a physical file dynamically, at run time. Normally, you assign the physical file statically in the ASSIGN clause by naming the file in the TO phrase. With the USING phrase of the ASSIGN clause, instead of specifying the name of the physical file, you specify a data item to contain the name of the physical file. You can then change the value of the data item at run time to open different physical files. However, for each logical file, only one physical file can be open at a time. For more information on the ASSIGN clause, see the HP COBOL II/XL Reference Manual. Example. The following program uses dynamic file assignment. The program reads a file name from the terminal, opens the file, and displays its contents one screen at a time. It then reads another file name from the terminal and displays that file. This program can display any number of files with like fixed file attributes. 001000 IDENTIFICATION DIVISION. 001100 PROGRAM-ID. DYNFILE. 001200 ENVIRONMENT DIVISION. 001300 INPUT-OUTPUT SECTION. 001400 FILE-CONTROL. 001500 SELECT INFILE ASSIGN USING FILE-NAME 001600 FILE STATUS IS INFILE-STAT. 001700 DATA DIVISION. 001800 FILE SECTION. 001900 FD INFILE. 002000 01 IN-RECORD PIC X(80). 002100 WORKING-STORAGE SECTION. 002200 01 FILE-NAME PIC X(9) VALUE SPACES. 002300 88 NO-MORE-FILES VALUE "//". 002400 01 SCREEN-SIZE PIC 99 VALUE ZERO. 002500 88 SCREEN-FULL VALUE 22. 002600 01 RETURN-KEY PIC X. 002700 01 INFILE-STAT PIC XX. 002800 88 INFILE-EOF VALUE "10". 002900 88 INFILE-NOT-THERE VALUE "35". [REV END] [REV BEG] 003000 PROCEDURE DIVISION. 003100 FIRST-PARA. 003200 PERFORM UNTIL NO-MORE-FILES 003300 MOVE SPACES TO FILE-NAME, INFILE-STAT 003400 DISPLAY "Enter the name of the file to list." 003500 DISPLAY "Enter // if no more files: " 003600 WITH NO ADVANCING 003700 ACCEPT FILE-NAME 003800 IF NO-MORE-FILES 003900 STOP RUN 004000 ELSE 004100 PERFORM GET-FILE 004200 END-IF 004300 END-PERFORM. 004400 004500 GET-FILE. 004600 OPEN INPUT INFILE 004700 EVALUATE INFILE-STAT 004800 WHEN "35" DISPLAY "Could not find the file." 004900 WHEN "00" PERFORM DISPLAY-FILE 005000 WHEN OTHER 005100 DISPLAY "An error occurred while opening the file." 005200 END-EVALUATE. 005300 005400 DISPLAY-FILE. 005500 PERFORM UNTIL INFILE-EOF 005600 MOVE ZERO TO SCREEN-SIZE 005700 PERFORM UNTIL SCREEN-FULL 005800 READ INFILE 005900 AT END SET SCREEN-FULL TO TRUE 006000 NOT AT END 006100 DISPLAY IN-RECORD WITH NO ADVANCING 006200 ADD 1 TO SCREEN-SIZE 006300 END-READ 006400 END-PERFORM 006500 DISPLAY "Press Return: " WITH NO ADVANCING 006600 ACCEPT RETURN-KEY 006700 END-PERFORM 006800 CLOSE INFILE. [REV END] [REV BEG] Below is sample output from the above program. Enter the name of the file to list. The program asks for a file name. Enter // if no more files: FILE1 File name is FILE1. Here is line 1 of FILE1. Contents of FILE1. Here is line 2 of FILE1. Contents of FILE1. Press Return: End of FILE1. Enter the name of the file to list. The program asks for another file name. Enter // if no more files: FILEA File name is FILEA. Here is line 1 of FILEA. Contents of FILEA. Here is line 2 of FILEA. Contents of FILEA. Here is line 3 of FILEA. Contents of FILEA. Here is line 4 of FILEA. Contents of FILEA. Press Return: End of FILEA. Enter the name of the file to list. The program asks for another file name. Enter // if no more files: FILE2 File name is FILE2. Could not find the file. FILE2 does not exist. Enter the name of the file to list. The program asks for another file name. Enter // if no more files: FILE3 File name is FILE3. An error occurred while opening the file. FILE3 has 72-character records. Enter the name of the file to list. The program asks for another file name. Enter // if no more files: // Enter // to end the program. : Return to the MPE XL prompt. [REV END] Multiple Files on a Labelled Tape Two obsolete features of the ANSI 1985 Standard allow you to sequentially access multiple physical files on a labelled tape, without rewinding the tape. They are the MULTIPLE FILE TAPE clause in the I-O CONTROL paragraph and the VALUE OF clause in the file descriptor. Either one, used in conjunction with the CLOSE WITH NO REWIND statement, allows such access. Because the MULTIPLE FILE TAPE and VALUE OF clauses are obsolete, a file equation of the following form is recommended instead: FILE filename; LABEL=volume_id, type, expiration_date, NEXT As with the obsolete clauses, the above file equation may be used in conjunction with the CLOSE WITH NO REWIND statement.


MPE/iX 5.0 Documentation