View previous topic :: View next topic |
Author |
Message |
christyleomin
Joined: 08 Apr 2011 Posts: 155
|
Posted: Fri Oct 14, 2011 10:44 am Post subject: File existence - please help |
|
|
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
-----
N) file N
Now, I successively open files using;
Code: | 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? |
|
Back to top |
|
|
JohnCampbell
Joined: 16 Feb 2006 Posts: 2554 Location: Sydney
|
Posted: Fri Oct 14, 2011 11:03 am Post subject: |
|
|
You could use the following function, to test if the file exists, before calling open, such as:-
Code: |
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
|
|
|
Back to top |
|
|
PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 7925 Location: Salford, UK
|
Posted: Fri Oct 14, 2011 11:46 am Post subject: |
|
|
There is also the functin FEXISTS@ in the FTN95 library. |
|
Back to top |
|
|
christyleomin
Joined: 08 Apr 2011 Posts: 155
|
Posted: Fri Oct 14, 2011 1:56 pm Post subject: |
|
|
Does INQUIRE statement work in Fortrann 77 as well because the code has to be replicated in F77 as well.. |
|
Back to top |
|
|
Wilfried Linder
Joined: 14 Nov 2007 Posts: 314 Location: Düsseldorf, Germany
|
|
Back to top |
|
|
christyleomin
Joined: 08 Apr 2011 Posts: 155
|
Posted: Sat Oct 15, 2011 4:55 pm Post subject: |
|
|
Thanks to all.It helps a lot! |
|
Back to top |
|
|
johannes
Joined: 21 Jan 2011 Posts: 65 Location: Leimen, Germany
|
Posted: Wed Nov 23, 2011 6:47 pm Post subject: |
|
|
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 |
|
Back to top |
|
|
|