program exf7 ! exf7.f90 read and write files implicit none ! must define all integers integer :: iostat, stat, dat, i integer :: n = 4 character(len=10) :: aformat = "(a20/)" ! format a for characters, f7.3 or e15 for real, i for integer, 1x for space print *, "exf7.f90 read and write files" write(6,fmt="(a20)") "unit 6 is std output" ! read(5,fmt="(i5)", number) ! from std input write(6, aformat) "unit 5 is std input " print *, "writing myfile.dat, then read" ! status="new", status="replace", status="write, options open(unit=11, file="myfile.dat", & form="formatted", action="readwrite", iostat=iostat) print *, "iostat=", iostat if(iostat /= 0) then print *, "can not open file: myfile.dat for writing" stop end if write(unit=11, fmt="(i1)") n print *, "writing ", n do i=1,n write(unit=11, fmt="(i2)") n*10 print *, "writing ", i*10 end do close(unit=11, status="keep") print *, "reading myfile.dat, that was just written" open(unit=11, file="myfile.dat", action="read", iostat=iostat) if(iostat /= 0) then print *, "can not open file: myfile.dat for reading" stop end if do read(unit=11, fmt="(i1)", iostat=iostat) n if(iostat < 0) exit ! end of file if(iostat > 0) cycle ! bad data print *, "n=", n do i=1,n read(unit=11, fmt="(i2)", iostat=iostat) dat if(iostat /= 0) print *, "may have bad data" if(iostat == 0) print *, "dat=", dat end do end do close(unit=11, status="keep") print *, "exf7.f90 finished" end program exf7