1

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?

4
  • 1
    How is the file connected, ie., what open statement is there? It's almost certainly an unformatted sequential access file, and the record is of length around that number. Commented Apr 27, 2016 at 20:01
  • yes, indeed it is unformatted: open (unit=unitname,file='name.ext',form='unformatted',status='old',iostat=ios) Does the data follow the record number? Commented Apr 27, 2016 at 20:18
  • The record length is embedded in the file, see here : stackoverflow.com/a/15071797/1004168 Commented Apr 27, 2016 at 20:24
  • As explained already, Fortran is skipping a single record. Most likely, the record has 2476 data bytes from whatever data items were written by the program that created it and 8 additional bytes, 4 pre and 4 post, specifying the length. e.g., see stackoverflow.com/questions/15190092/… You can check the value in the first 4 and last 4 bytes to confirm. Commented Apr 28, 2016 at 1:35

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.