user1667674
user1667674

Reputation:

status clause in open statement showing strange behavior

Hi here is my fortran code

program test

implicit none

INTEGER :: ierror
character:: filename

filename="fun"


open(unit=25,file=filename ,status="replace",action="write", &
           iostat=ierror)


              write(*,*)ierror

end program test 

I am using Chapman's book to learn Fortran 95-2003. According to him (page 219)status='replace' clause will open a new file with the name fun . If there is any file with such name, then it is deleted. But I created the file fun in the home directory where Fortran program is stored, and then ran this program. It did create a new file with name f. The file fun was not deleted. So I don't understand this behavior.... Any help appreciated......

Upvotes: 1

Views: 143

Answers (1)

High Performance Mark
High Performance Mark

Reputation: 78364

You have declared variable filename to have type character. Since you've not specified a length the compiler understands it to have length 1, so your assignment

filename="fun"

leads to filename having the value f. Change your declaration of the variable to

character(len=3) :: filename

or, probably better,

character(len=:), allocatable :: filename

The latter version uses modern (2003 and later I think) Fortran's automatic allocation capabilities.

Upvotes: 3

Related Questions