! direct.f90 ! ! write and read values of a derived type ! use direct access, unformatted input/output on a disk file ! ! 11/18/96 JSS initial version program direct implicit none integer, parameter :: n = 100 integer :: i integer :: iostat integer :: recl type name character (len=30) :: last character (len=30) :: first character (len=30) :: middle end type name type address character (len=40) :: street character (len=40) :: more character (len=20) :: city character (len=2) :: state integer (selected_int_kind(5)) :: zip_code integer (selected_int_kind(4)) :: route_code end type address ! could be many more types of components type person type (name) lfm type (address) snail_mail end type person type (person) :: a_person = person(name("Squire","Jon","S."), & address("106 Regency Circle", "", "Linthicum", "MD", 21090, 1936)) inquire(iolength=recl) a_person ! get recl for the open statement print *, "recl=", recl open(unit=11, file="direct.dat", status="replace", access="direct", & form="unformatted", recl=recl, action="readwrite", iostat=iostat) print *, "iostat=", iostat print *, "direct starting to write 1, 3, 5, ... n" do i=1,n,2 a_person%snail_mail%route_code = i write(unit=11, rec=i, iostat=iostat) a_person if(iostat /= 0) print *, "iostat=", iostat end do print *, "direct starting to write 2, 4, 6, ... n" do i=2,n,2 a_person%snail_mail%route_code = i write(unit=11, rec=i, iostat=iostat) a_person if(iostat /= 0) print *, "iostat=", iostat end do print *, "direct starting to read n, n-1, ... 1" do i=n,1,-1 read(unit=11, rec=i, iostat=iostat) a_person print *, a_person%snail_mail%route_code, i, iostat end do close(unit=11, status="keep") print *, "direct finished" end program direct