How Delete File from Fortran Code

Delete data of file

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)

Fortran: how to remove file extension from character

You can use scan to locate the position of the last dot in the string input_file. Then you can use that position to extract the input_file with no extension and concatenate the new one.

character(len=256):: input_file, output_file
integer :: ppos
character(len=3) :: new_ext="csv"

ppos = scan(trim(input_file),".", BACK= .true.)
if ( ppos > 0 ) output_file = input_file(1:ppos)//new_ext

Write in an existing file with overwriting and append in Fortran

After all the discussion you had with other users, I just think that (as mentioned) you are overcomplicating things here.

The following code (untested and using you formatting...) should give you what you want. If you want to do fancy things with the filename (which was a bit unclear in the discussion) then you may need to do something else.

  program test
double precision f
dimension f(10,10)
integer i,j

OPEN(61,file="file.txt",action='write',status='replace')
do i=1,2
do j=1,2
f(i,j)= i+10d0*j
WRITE(61,*) f(i,j)
enddo
enddo
CLOSE(61)

stop
end

This will give you always the output you desire in file.txt at every run. So you open the file once (replacing any previous version), write the f as many time as the double loop iterates on (writing each record of f after the previous one in the same file, and I think here is where you are getting confused with append), and close the file.

EDIT

With the new example you posted the problem is now clear. This would have helped and avoided a lot of time if you could have explained in the original post.

A way to solve it is to add a condition inside the loop, where it checks if it is the first iteration of the loop or not (for each k value). Note also that the k loop needs to iterate the fastest (inner loop).

  program test
double precision f
dimension f(10,10,10)
integer i,j,k
character*500 ofile

do i=1,2
do j=1,2
do k=1,5
f(i,j,k)= i+10d0*j+k
WRITE(ofile,'(A,F4.2,A,F4.2,A)')'file',i*1d0,'_',j*1d0,'.dat'
if (k.eq.1) then
OPEN(61,file=ofile,action='write',status='replace')
else
OPEN(61,file=ofile,action='write',position='append')
end if
WRITE(61,*)k,f(i,j,k)
CLOSE(61)
enddo
enddo
enddo
stop
end

If you want to stick to the idea of just removing the files before each run just add call system('rm file*') before starting the loops (this is for a Unix terminal).

Close multiple files

I personally wouldn't use the end statement, instead I would call a subroutine that closes the correct file:

subroutine del_file(uFile, stat)
implicit none
integer uFile, stat

c If the unit is not open, stat will be non-zero
close(unit=uFile, status='delete', iostat=stat)
end subroutine

You're read statement then would be:

read(unit=curUnit, iostat=stat) w
if ( stat .ne. 0 ) call del_file(curUnit, stat)

You still would need some logic in there not to read from close files. I would recommend an array to hold all the units corresponding to the input files.

dispose syntax error when using fortran open statement

Dispose is a non-standard compiler extension (and not supported by your compiler). As described in this answer, the standard way to do this is to delete the file on closure:

f = "espy.tmp";  h = "formatted";  r = "read" 
Open (newunit=u, file=f, form=h, action=r, &
status="old")

close(u, status='delete')

Or, you could use temporary/scratch files (no filename):

f = "espy.tmp";  h = "formatted";  r = "read" 
Open (newunit=u, form=h, action=r, &
status="old", status='scratch')


Related Topics



Leave a reply



Submit