Stream access: problem with pos= specifier


I encountered problems using stream access with pgf90, (version 11.3,linux 64 bits)

The program is the following:
(myfile is a file of 15 bytes)

program a
implicit none
character :: c
integer :: ios
character(200) :: message

read(1,pos=20,iostat=ios,iomsg=message) c
write(6,) ‘ios1=’,ios
) ‘mess1=’,trim(message)
read(1,pos=0,iostat=ios) c
write(6,) ‘ios2=’,ios
) ‘mess2=’,trim(message)

end program a

The output is the following:

ios1= -1
attempt to read past end of file
ios2= -1
attempt to read past end of file

I understand the values of ios1 and mess1, since the first read statement
tried to read after the end of the file. However, I do not understand
the values of ios2 and mess2.

The pos=0 specifier in the second read statement doesn’t seem to be taken into account.
It seems that, as soon as the end of the file has been reached, is it impossible
to read again the file.

Is this a bug ?

This problem can be solved

  • by closing and reopening the file between the two read statements
  • by adding a rewind statement.

thank you for your answers and/or comments,


Note that, in Fortran stream access I/O, the pos value is 1 for formatted files, not 0, at the beginning of the file.

Thank you for your remark.

I made a test with pgf90 v11.3. If I specify pos=1, I read the second byte of the file, if I specify
pos=0, I read the first byte of the file. I do not know what the Fortran 2003 standard specifies.


Stream file position, if available, is measured in "file storage unit"s, and this unit need not be a byte – it is compiler dependent. The iso-fortran-env module provides the named constant file_storage_size for use in determining the file position.

In my case file_storage_size=8, thus the file storage unit was the byte.

Is this a bug ?

I believe this is the correct behavior. Per the F2003 Standard section, “For formatted stream input, if an end-of-file condition occurred, the file position is not changed”. In other words, once you reach an EOF, the file pointer will remain in an EOF state. Granted, you are using unformatted stream input so the correct behavior may be to position the file pointer after the EOF, but in either case the READ will be in an error state.

  • Mat