Silverfrost Forums

Welcome to our forums

File existence - please help

14 Oct 2011 9:44 #9088

I am reading several files stored at a particualr location.

There might be 'N' number of files which are ALWAYS named as;

  1. file01 2)file 02 3)---

10)file10


  1. file N

Now, I successively open files using;

open(unit=1,
     1     file='file01',
     2     status='old',
     3     form='unformatted',
     4     access='direct',
     5     recl=512*8)

...do calculations

then open next file

...do calcualtions

..and so on

----

How do I find (before opening a file- say file06) whether it exists or not.

For the first file, the user specifies the file name and location, but it is the program which needs to find whether file02 exists and do coresponding calcualtions.

How could I find before the open statement whether file 02 exists or not?

14 Oct 2011 10:03 #9089

You could use the following function, to test if the file exists, before calling open, such as:-

   if ( file_exists (File_Name) ) Then
      OPEN (file=File_name,           &
            Status='OLD',             &
            form='unformatted',       &
            access='direct',          &
            recl=512*8,               &
            IOSTAT=IOSTAT,
.....
      LOGICAL FUNCTION File_Exists (FILE_NAME)
!
!   Tests for the existence of a file
!
      character (len=*),        intent (in)     :: file_name
!
      LOGICAL   EXIST
      INTEGER*4 IOSTAT
!
      INQUIRE (FILE = FILE_NAME, EXIST = EXIST, IOSTAT = IOSTAT )
      IF (IOSTAT /= 0) EXIST = .FALSE.
!
      file_exists = EXIST
!
      END
14 Oct 2011 10:46 #9090

There is also the functin FEXISTS@ in the FTN95 library.

14 Oct 2011 12:56 #9092

Does INQUIRE statement work in Fortrann 77 as well because the code has to be replicated in F77 as well..

14 Oct 2011 2:55 #9093

Yes. For instance, look here: http://www.univ-orleans.fr/EXT/ASTEX/astex/doc/en/prof77/html/prof77.htm

Regards - Wilfried

15 Oct 2011 3:55 #9095

Thanks to all.It helps a lot!

23 Nov 2011 5:47 #9282

Just to add a comment, how to automatically fill the correct file names, esp. file02 etc. in other words how to make the leading zeros for file number <10.

For this purpose use the In.m descriptor, which adds leading zeros

character*6 filename do i=1,20 write (filename,'(A4,I2.2)') 'file',i enddo

Please login to reply.