2
\$\begingroup\$

The Fortran program 'MAIN' below calls 3 subroutines.

  1. It opens a file if it is not present, (overwrites if it is already present).
  2. writes something on the file using the file identifier integer and
  3. closes the file.

       program MAIN

2001 FORMAT (A,I10)
   INTEGER FILEIDS(10)
   CHARACTER*64 fname
   FILEIDS(1) = 701
   write(*,2001) 'opnoutfl', FILEIDS(1)

   fname = 'advect1.dat'

   CALL OFNFL (FILEIDS(1), fname)

   CALL BDEXCH (FILEIDS(1))
   CALL CLOOUTFL(FILEIDS(1))
   END



   SUBROUTINE OFNFL (UNITNO, fname)

   LOGICAL EXISTS, OPND

   INTEGER UNITNO
   CHARACTER*64 fname,dirname,path

2001 FORMAT (A,I10)
     write(*,2001) 'opnoutfl', UNITNO
    dirname = 'sediment/'
    path  = trim(dirname) // trim(fname)

   INQUIRE (file=path, EXIST=EXISTS)

  IF (EXISTS) THEN
     OPEN (UNITNO, file=path, status='old')
     close(UNITNO, STATUS='DELETE')
     OPEN (UNITNO, file=path, status='NEW')
     WRITE(UNITNO,2001) 'Alte Dateil ist geloest  ',UNITNO
  ELSE
     OPEN (UNITNO, file=path, status='NEW')
     WRITE(UNITNO, 2001) 'Neue Datei ist geoeffnet',UNITNO
  END IF

    RETURN
    END


  SUBROUTINE CLOOUTFL (UNITNO)

  INTEGER UNITNO

  CLOSE(UNITNO)
  RETURN
  END


   SUBROUTINE BDEXCH (MESSU)

   INTEGER MESSU

2001 FORMAT (A,I10)    

   WRITE(MESSU, 2001) 'bdexch schreibt', MESSU+10

    RETURN
    END

My question is, at this moment I am passing file identifier as argument. How can I modify/improve this code so that the file identifier created in first subroutine i.e. OFNFL() be automatically seen to subsequent subroutines which uses/writes on the file? The program where I want to use it creates lots of files and then different subroutines writes on different files, so passing file identifier like this will not look very clean!!

\$\endgroup\$
  • \$\begingroup\$ I would advise to have a look at he MODULE facilities of Fortran as well as at the CONTAINS statement. \$\endgroup\$ – albert Aug 9 '18 at 12:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.