HP 3000 Manuals

Using the File Handling Procedures [ HP FORTRAN 77/iX Programmer's Guide ] MPE/iX 5.0 Documentation


HP FORTRAN 77/iX Programmer's Guide

Using the File Handling Procedures 

This section describes the FSET, FNUM, and UNITCONTROL procedures.  Refer
to the HP FORTRAN 77/iX Reference Manual for more details on these
procedures.

FSET Procedure 

The FSET procedure changes the MPE/iX operating system file number
assigned to a given FORTRAN logical unit number in the file table.

The FSET procedure can be called from a FORTRAN 77 program as follows:

     CALL FSET(unit,newfile,oldfile)

------------------------------------------------------------------------------
|              |                                  |                          |
|     Item     |       Description/Default        |       Restrictions       |
|              |                                  |                          |
------------------------------------------------------------------------------
|              |                                  |                          |
| unit         | Positive integer constant or     | Must be nonnegative.     |
|              | variable (INTEGER*2 or           |                          |
|              | INTEGER*4) that specifies the    |                          |
|              | FORTRAN file unit for which the  |                          |
|              | change is to be made.            |                          |
|              |                                  |                          |
------------------------------------------------------------------------------
|              |                                  |                          |
| newfile      | Positive integer constant or     | Must be nonnegative.     |
|              | variable (INTEGER*2 or           |                          |
|              | INTEGER*4) that specifies the    |                          |
|              | new MPE/iX file number to be     |                          |
|              | assigned to unit.                |                          |
|              |                                  |                          |
------------------------------------------------------------------------------
|              |                                  |                          |
| oldfile      | Integer variable to which the    | Cannot be the same       |
|              | procedure returns the old value  | variable as newfile.     |
|              | of the file number that was      |                          |
|              | assigned to unit.                |                          |
|              |                                  |                          |
------------------------------------------------------------------------------

The following program shows how to use the FSET procedure to assign a
FORTRAN logical unit number.

     $WARNINGS OFF
     $SHORT

           PROGRAM fset_example
     C
     C  FOPEN, FSET, and FCLOSE Example

           IMPLICIT NONE
           INTEGER filenumber,oldnum
           SYSTEM INTRINSIC FOPEN,FCLOSE
           CHARACTER buffer*72,filename*16
           PARAMETER (FILENAME = 'maillist')

           filenumber = FOPEN(filename,1B,105B)
           IF (ccode())30,10,30

     C  Call FSET to assign the FORTRAN unit number five to "filenumber"

     10    CALL FSET(5,filenumber,oldnum)
           PRINT *,'Old file number = ',oldnum
           PRINT *,'FOPEN number = ',filenumber
     20    READ(5, '(A72)' ,END=40)buffer       ! Read to EOF
           WRITE(6,100)buffer(1:19)
     100   FORMAT(T2,A20)
           GO TO 20
     30    PRINT *,'Could not open file'
           STOP

     C  Close the file

     40    CALL FCLOSE(filenumber,1B,0B)
           IF (ccode())50,60,50
     50    PRINT *,'Could not close file'
           STOP
     60    PRINT *,'File closed successfully'
           STOP
           END

This is the output of the program:

      Old file number =  11
      FOPEN number =  10
       SMILEY    FACE
       MICKEY    MOUSE
       SLIM      JIM
       CHARITY   BELL
       DONALD    DUCK
       JOE       SMOE
       CLAIRE    PLIMSOL
       INDIANA   JONES
       JAKE      FAKE
      File closed successfully

FNUM Procedure 

The FNUM procedure returns the MPE/iX system file number assigned to a
given FORTRAN 77 logical unit number.  This procedure returns an
INTEGER*2 or INTEGER*4 value.

The FNUM procedure can be called from an HP FORTRAN 77/iX program as an
external function as follows:

           I = FNUM(unit)

------------------------------------------------------------------------------------
|              |                                        |                          |
|     Item     |          Description/Default           |       Restrictions       |
|              |                                        |                          |
------------------------------------------------------------------------------------
|              |                                        |                          |
| unit         | Positive integer (INTEGER*2 or         | Must be nonnegative.     |
|              | INTEGER*4) that specifies the FORTRAN  |                          |
|              | file unit for which the MPE/iX system  |                          |
|              | file number is to be extracted.  The   |                          |
|              | value returned is the same size as the |                          |
|              | argument.                              |                          |
|              |                                        |                          |
------------------------------------------------------------------------------------

See the UNITCONTROL procedure description below for an example of using
the FNUM procedure.

UNITCONTROL Procedure 

The UNITCONTROL procedure allows an HP FORTRAN 77/iX program to request
several actions (see below) for any FORTRAN 77 logical unit.

The UNITCONTROL procedure is called as follows:

     CALL UNITCONTROL(unit,opt)

------------------------------------------------------------------------------------
|              |                                        |                          |
|     Item     |          Description/Default           |       Restrictions       |
|              |                                        |                          |
------------------------------------------------------------------------------------
|              |                                        |                          |
| unit         | Positive integer (INTEGER*2 or         | Must be nonnegative.     |
|              | INTEGER*4) that specifies the unit     |                          |
|              | number of the file to be used.         |                          |
|              |                                        |                          |
------------------------------------------------------------------------------------
|              |                                        |                          |
| opt          | Integer (INTEGER*2 or INTEGER*4) that  | None.                    |
|              | specifies the option (see Table 4-1).  |                          |
|              |                                        |                          |
------------------------------------------------------------------------------------

The options for the UNITCONTROL intrinsic are listed in Table 4-1 .

          Table 4-1.  UNITCONTROL Options 

---------------------------------------------------------
|              |                                        |
|    Option    |              Description               |
|              |                                        |
---------------------------------------------------------
|              |                                        |
|      -1      | Rewind (but don't close the file)      |
|              |                                        |
|      0       | Backspace                              |
|              |                                        |

|      1       | Write an EOF mark.                     |
|              |                                        |
|      2       | Skip backward to a tape mark           |
|              |                                        |
|      3       | Skip forward to a tape mark            |
|              |                                        |
|      4       | Unload the tape and close the file     |
|              |                                        |
|      5       | Leave the tape loaded and close the    |
|              | file                                   |
|              |                                        |
|      6       | Convert the file to prespacing         |
|              |                                        |
|      7       | Convert the file to postspacing        |
|              |                                        |
|      8       | Close the file                         |
|              |                                        |
---------------------------------------------------------


NOTE Use the REWIND, BACKSPACE, ENDFILE, and CLOSE statements instead of the -1, 0, 1, and 8 options. These statements are part of the FORTRAN 77 language and are more portable. Option values outside the range of -1 through 8 are ignored and no action is taken.
The program below shows how to use the FNUM and UNITCONTROL procedures. The SHORT compiler directive forces the integer and logical default size to two bytes. $WARNINGS OFF $SHORT PROGRAM unit_fnum_ex C Example program using FNUM and UNITCONTROL IMPLICIT NONE SYSTEM INTRINSIC HPMERGEINIT CHARACTER buffer*72 INTEGER*4 keysonly,numkeys,keys(8),infiles(3),status INTEGER*4 outputfile(2),error C Merge two sorted files, MAIL1 (unit 20) and MAIL2 (unit 21) C into a third file, MAIL3 (unit 22) C Open all files OPEN(20,FILE='mail1',STATUS='OLD',ERR=200) OPEN(21,FILE='mail2',STATUS='OLD',ERR=300) OPEN(22,FILE='mail3',STATUS='NEW',ERR=400) C Establish keys for SORT - major at column 11 for 9 bytes C (LAST NAME) and minor at column 1 for 10 bytes (FIRST NAME) keys(1) = 11 keys(2) = 9 keys(3) = 10 keys(4) = 0 keys(5) = 1 keys(6) = 10 keys(7) = 10 keys(8) = 0 keysonly = 0 numkeys = 2 C Establish MPE/iX filenumbers for input files (MAIL1 and MAIL2) C by referencing the FNUM procedure infiles(1) = FNUM(20) infiles(2) = FNUM(21) infiles(3) = 0 C Establish MPE/iX filenumbers for output file (MAIL3) by C referencing the FNUM procedure outputfile(1) = FNUM(22) outputfile(2) = 0 C Merge the files CALL HPMERGEINIT(status,infiles,,outputfile,, > keysonly,numkeys,keys,,,,,error) IF (error .NE. 0) STOP 'Merge failed' C Display the new merged file REWIND 22 20 READ(22,'(A72)',END=30) buffer WRITE(6,100)buffer 100 FORMAT(T2,A) GO TO 20 C Call UNITCONTROL to close the files MAIL1, MAIL2, and MAIL3, C which is the same as using the CLOSE statement 30 CALL UNITCONTROL(20,8) CALL UNITCONTROL(21,8) CALL UNITCONTROL(22,8) STOP 200 PRINT *,'Could not open file - MAIL1' STOP 300 PRINT *,'Could not open file - MAIL2' STOP 400 PRINT *,'Could not open output file - MAIL3' END This is the output of the program: PLAINS ANTELOPE 201 OPENSPACE AVE BIGCOUNTRY WY LOIS ANYONE 6190 COURT ST METROPOLIS NY KING ARTHUR 329 EXCALIBUR ST CAMELOT CA ALI BABA 40 THIEVES WAY SESAME CO BLACK BEAR 47 ALLOVER DR ANYWHERE US JOHN BIGTOWN 965 APPIAN WAY METROPOLIS NY KNEE BUCKLER 974 FISTICUFF DR PUGILIST ND SWASH BUCKLER 497 PLAYACTING CT MOVIETOWN CA ANIMAL CRACKERS 1000 CRUNCH LN COOKIE US MULE DEER 963 FOREST PL HIGHCOUNTRY CA WHITETAIL DEER 34 WOODSY PL BACKCOUNTRY ME JAMES DOE 4193 ANY ST ANYTOWN MD JANE DOE 3959 TREEWOOD LN BIGTOWN MA PRAIRIE DOG 493 ROLLINGHILLS DR OPENSPACE ND JOHN DOUGHE 239 MAIN ST HOMETOWN MA MALLARD DUCK 79 MARSH PL PUDDLEDUCK CA JENNA GRANDTR 493 TWENTIETH ST PROGRESSIVE CA KARISSA GRANDTR 7917 BROADMOOR WAY BIGTOWN MA SNOWSHOE HARE 742 FRIGID WAY COLDSPOT MN MOUNTAIN LION 796 KING DR THICKET NM SPACE MANN 9999 GALAXY WAY UNIVERSE CA SWAMP RABBIT 4444 DAMPLACE RD BAYOU LO NASTY RATTLER 243 DANGER AVE DESERTVILLE CA BIGHORN SHEEP 999 MOUNTAIN DR HIGHPLACE CO GREY SQUIRREL 432 PLEASANT DR FALLCOLORS MA


MPE/iX 5.0 Documentation