user3180077
user3180077

Reputation: 633

Read a binary file with Fortran - if no input list provided, how many bytes does Fortran skip?

I'm trying to understand some Fortran code. At one point there is line where it reads a binary file without specifying any input list, just the file itself and a statement label for reaching the end of the file:

open (unit=unitname,file='name.ext',form='unformatted',status='old',iostat=ios)
...
read (myFile,end=902)

I read the file with some Python code and with some debugging, I realized that the Fortran code skips exactly 2484 bytes (yes, I counted!) with this read command. I don't know if there is a special reason for this. If I'm not mistaken, a read command in Fortran would simply read the whole line without any input list, but as this is a binary file, I wonder what happens then. Where does this 2484 magic number come from? What happens when you read a binary file without specifing an input list in Fortran?

Upvotes: 1

Views: 344

Answers (1)

francescalus
francescalus

Reputation: 32366

For a file connected for sequential access, a read statement with no input items advances the position of the file by a record.

For formatted input, as you note in the question, such a read would skip a line: in a file for this, a record is generally a line.

The same idea holds for unformatted input, from what you're calling a binary file. What is meant by a record here is a little beyond the scope of this answer perhaps (and there are lots of nuances around this), but the crucial thing to note is that there is still a well-defined notion of a record's size.

And to fully justify the statement, your file is indeed connected for unformatted transfer (and is compatible with that read statement):

open (unit=unitname,file='name.ext',form='unformatted',status='old',iostat=ios)

Without an access= specifier to the contrary in that open the mode is sequential.

Upvotes: 2

Related Questions