Gao
Gao

Reputation: 103

what's the meaning of "iostat" argument in open statement?

I am confused by the use of the 'iostat' argument in open file. As it's said, when the open command succeeds, the 'iostat' gets a value of 0.

open(unit=99, file='vel_zcor22.txt', status='old', iostat=ierr, err=100)
100 if(ierr .ne. 0) then
    print*, 'open file error'
endif   
print*, ierr 

Why is not the 'iostat' used to tell the state rather than the 'ierr'. As my understanding of assignment operator, the 'ierr' transfers its value to 'iostat'. So what is the role of the 'ierr' playing in this procedure?

Upvotes: 2

Views: 1857

Answers (1)

francescalus
francescalus

Reputation: 32451

In an open statement, the iostat=ierr is using iostat= as a specifier. It is not an assignment, transferring the value of ierr to the variable iostat.

Much like when using keywords in a subroutine or function reference (where call sub(a=x) associates the actual argument x with the dummy argument a), what is happening is more:

use the variable ierr to store the resulting status of the statement.

So, when "iostat gets a value of 0" what really happens is the variable ierr becomes defined.

You could instead use any variable name instead of ierr, and typically one often uses iostat:

open(..., iostat=iostat, ...)

Equally, the other parts you see aren't assignments either. That is:

open(unit=99, file='vel_zcor22.txt', status='old', iostat=ierr, err=100)

may look like assignments, but it's still saying:

open on unit 99, this file, with status 'old', passing control to statement labelled 100 if there's an error.

It isn't setting a variable unit to 99, etc.

Upvotes: 2

Related Questions