fortran binary
时间: 2023-09-28 10:10:16 浏览: 109
Fortran is a programming language that can be used to write binary files. Binary files are files that contain non-textual data, such as images, audio, or executable code.
To write binary files in Fortran, you can use the "OPEN" statement with the "ACCESS='STREAM'" option. This allows you to read and write binary data to and from the file.
Here's an example program that writes binary data to a file:
```
program write_binary_file
implicit none
integer :: i, n
real :: x(100)
character(len=20) :: filename
integer, parameter :: file_unit = 10
! Set the file name
filename = 'data.bin'
! Open the file for writing
open(unit=file_unit, file=filename, access='stream', form='unformatted', status='replace')
! Write some data to the file
n = 100
do i = 1, n
x(i) = i**2
end do
write(file_unit) n, x(1:n)
! Close the file
close(file_unit)
end program write_binary_file
```
This program writes an array of 100 real numbers to a binary file named "data.bin". The first value written to the file is the number of elements in the array, followed by the array itself. The "form='unformatted'" option tells Fortran to write the data in binary format.
To read binary files in Fortran, you can use the same "OPEN" statement with the "ACCESS='STREAM'" option, and then use the "READ" statement to read the data from the file.
阅读全文