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?

user3180077
  • 633
  • 1
  • 8
  • 16
  • 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. – francescalus Apr 27 '16 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? – user3180077 Apr 27 '16 at 20:18
  • The record length is embedded in the file, see here : http://stackoverflow.com/a/15071797/1004168 – agentp Apr 27 '16 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 http://stackoverflow.com/questions/15190092/how-do-i-read-fortran-binary-file-in-c You can check the value in the first 4 and last 4 bytes to confirm. – M. S. B. Apr 28 '16 at 01:35

1 Answers1

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.

francescalus
  • 30,576
  • 16
  • 61
  • 96