Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read/Write Binary

Status
Not open for further replies.

cosmoh2o

Programmer
Jul 22, 2002
92
US
Please help,

I am new to Fortran so this is probably a VERY simple question but what is the easiest way to write data to a binary file using Fortran?

Any help/hint/simple example code would be wonderful.

Thank you.
 
First open the file for an unformatted file with record length of 1.
Code:
          open (
     &        unit = fhand,  ! fhand is an integer set to a value
     &        file = iFilename,
     &        status = 'UNKNOWN',
     &        access = 'DIRECT', ! Otherwise it assumes a record header
     &        form = 'UNFORMATTED',
     &        recl = 1,
     &        err = 100) ! 100 is a label which tells you an error has occurred
When writing, indicate the record position
Code:
      write (fHand, rec=1) bfType, bfSize, bfReserved, bfOffBits
Last, close the file
Code:
      close (fHand)
On some implementations, a record length of 1 is not allowed. The minimum is probably 4. In that case rec=2 writes to byte 5 to 8.

You also have to watch out for the big endian little endian problem. In layman's terms, byte swapping. For instance, on some machines 513 is stored as 0201 hex, on other machines it is stored as 0102 hex. On machines where it is stored as 0102, the machine swaps them back to 0201 during processing.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top