You can do this with the inquire
statement:
logical :: file_exists
! ...
inquire(file='filename.txt',exist=file_exists)
if ( file_exists ) then
! Do stuff
else
! Do other stuff
endif
Or, since you are going to wipe the file anyway, just open it with status='replace'
;-) The difference between 'unknown'
and 'replace'
is, that 'replace'
will create a new file will if it doesn't exist.
For FORTRAN 77, status='replace'
does not exist. Then, the open
statement could read:
open(1234, file='filename.txt', status='unknown', iostat=ierr)
if ( ierr .eq. 0) then
c file opened successfully, delete
close(1234, status='delete')
endif
c Open a new file
open(1234, file='filename.txt', status='new', iostat=ierr)