Binary file operations in Fortran allow you to read from and write to files in binary format. This is particularly useful for handling non-text data, such as images, audio files, or any other data that is not easily represented as text. In this section, we will cover the basics of binary file operations, including opening files, reading from files, writing to files, and closing files.

Key Concepts

  1. Binary Files vs. Text Files:

    • Text Files: Store data in a human-readable format using characters.
    • Binary Files: Store data in a binary format, which is not human-readable but more efficient for certain types of data.
  2. File Handling Statements:

    • OPEN: Opens a file for reading or writing.
    • READ: Reads data from a file.
    • WRITE: Writes data to a file.
    • CLOSE: Closes an open file.
  3. File Access Modes:

    • FORM='BINARY': Specifies that the file is a binary file.
    • ACCESS='STREAM': Allows for unformatted, direct access to the file.

Practical Examples

Opening a Binary File

To open a binary file for reading or writing, you use the OPEN statement with the appropriate parameters.

PROGRAM OpenBinaryFile
  INTEGER :: unit_number
  CHARACTER(LEN=20) :: filename

  unit_number = 10
  filename = 'data.bin'

  OPEN(UNIT=unit_number, FILE=filename, FORM='BINARY', ACCESS='STREAM', STATUS='REPLACE')
  ! The file is now open for binary operations
  CLOSE(UNIT=unit_number)
END PROGRAM OpenBinaryFile

Writing to a Binary File

To write data to a binary file, you use the WRITE statement.

PROGRAM WriteBinaryFile
  INTEGER :: unit_number
  CHARACTER(LEN=20) :: filename
  INTEGER :: data(5) = [1, 2, 3, 4, 5]

  unit_number = 10
  filename = 'data.bin'

  OPEN(UNIT=unit_number, FILE=filename, FORM='BINARY', ACCESS='STREAM', STATUS='REPLACE')
  WRITE(UNIT=unit_number) data
  CLOSE(UNIT=unit_number)
END PROGRAM WriteBinaryFile

Reading from a Binary File

To read data from a binary file, you use the READ statement.

PROGRAM ReadBinaryFile
  INTEGER :: unit_number
  CHARACTER(LEN=20) :: filename
  INTEGER :: data(5)

  unit_number = 10
  filename = 'data.bin'

  OPEN(UNIT=unit_number, FILE=filename, FORM='BINARY', ACCESS='STREAM', STATUS='OLD')
  READ(UNIT=unit_number) data
  CLOSE(UNIT=unit_number)

  PRINT *, 'Data read from file:', data
END PROGRAM ReadBinaryFile

Closing a Binary File

Always ensure to close the file after completing the read or write operations to free up system resources.

CLOSE(UNIT=unit_number)

Practical Exercises

Exercise 1: Write and Read an Array

Task: Write a program that writes an array of real numbers to a binary file and then reads the array back from the file.

Solution:

PROGRAM WriteReadArray
  INTEGER :: unit_number
  CHARACTER(LEN=20) :: filename
  REAL :: data_write(5) = [1.1, 2.2, 3.3, 4.4, 5.5]
  REAL :: data_read(5)

  unit_number = 10
  filename = 'real_data.bin'

  ! Write data to binary file
  OPEN(UNIT=unit_number, FILE=filename, FORM='BINARY', ACCESS='STREAM', STATUS='REPLACE')
  WRITE(UNIT=unit_number) data_write
  CLOSE(UNIT=unit_number)

  ! Read data from binary file
  OPEN(UNIT=unit_number, FILE=filename, FORM='BINARY', ACCESS='STREAM', STATUS='OLD')
  READ(UNIT=unit_number) data_read
  CLOSE(UNIT=unit_number)

  PRINT *, 'Data read from file:', data_read
END PROGRAM WriteReadArray

Exercise 2: Binary File with Mixed Data Types

Task: Write a program that writes an integer, a real number, and a character to a binary file and then reads them back.

Solution:

PROGRAM WriteReadMixedData
  INTEGER :: unit_number
  CHARACTER(LEN=20) :: filename
  INTEGER :: int_data = 42
  REAL :: real_data = 3.14
  CHARACTER(LEN=1) :: char_data = 'A'
  INTEGER :: int_read
  REAL :: real_read
  CHARACTER(LEN=1) :: char_read

  unit_number = 10
  filename = 'mixed_data.bin'

  ! Write data to binary file
  OPEN(UNIT=unit_number, FILE=filename, FORM='BINARY', ACCESS='STREAM', STATUS='REPLACE')
  WRITE(UNIT=unit_number) int_data, real_data, char_data
  CLOSE(UNIT=unit_number)

  ! Read data from binary file
  OPEN(UNIT=unit_number, FILE=filename, FORM='BINARY', ACCESS='STREAM', STATUS='OLD')
  READ(UNIT=unit_number) int_read, real_read, char_read
  CLOSE(UNIT=unit_number)

  PRINT *, 'Integer read from file:', int_read
  PRINT *, 'Real number read from file:', real_read
  PRINT *, 'Character read from file:', char_read
END PROGRAM WriteReadMixedData

Common Mistakes and Tips

  • Forgetting to Close Files: Always close files after operations to avoid resource leaks.
  • Incorrect File Access Mode: Ensure you use FORM='BINARY' and ACCESS='STREAM' for binary files.
  • Data Type Mismatch: Ensure the data types used in WRITE and READ statements match the data being written and read.

Conclusion

In this section, you learned how to perform binary file operations in Fortran, including opening, reading, writing, and closing binary files. These skills are essential for handling non-text data efficiently. Practice the exercises provided to reinforce your understanding and prepare for more advanced file handling techniques.

© Copyright 2024. All rights reserved