More Example F Programs

Example of program that opens n unformatted files entitled "config1.dat, config2.dat, ... Note that if you try to see the contents of the files, it looks like gibberesh.
program unformatted_files
!  example program which opens n unformatted, direct access files
!  illustrate unformatted I/O
   integer :: i,j,n
   character(len = 15) :: file_name
   n = 4
   do i = 1,n
      write(unit=file_name,fmt="(i2.2,a)") i,".dat" 
!     // is concatenation operator
      file_name = "config"//file_name
!     record length is 8 for one double precision number
      open (unit=1,file=file_name,access="direct",recl=8, &
            form="unformatted",action="write",status="replace")
      do j = 1,3
         write (unit=1,rec=j) j*j
      end do
      close(unit=1)
   end do
end program unformatted_files
Note that use of the access, recl, and form specifiers. If the access is direct, recl must be specified. The latter specifies the length of each record if the access method is direct.

The following programs reads the jth record of the unformatted files generated by Program write_files.

program read_files
!  test program to open n files, write data, and close them.
!  illustrate unformatted I/O
   integer :: i,j,n,value
   character(len = 15) :: file_name
   n = 4
   j = 2     ! number of record
   do i = 1,n
      write(unit=file_name,fmt="(i2.2,a)") i,".dat" 
      file_name = "config"//file_name
      open(unit=1,file=file_name,access="direct",recl=8, &
           form="unformatted",action="read",status="old")
      read (unit=1,rec=j) value
      print *,value
      close(unit=1)
   end do
end program read_files
Note the specification of the record number in the read statement.

Please send comments and corrections to Harvey Gould, hgould@clarku.edu.

Updated 24 February 1998.