program read_csv implicit none integer, parameter :: dbl = selected_real_kind(p=14, r=99) integer :: iost=0 integer :: I1, I2, I3 real(dbl) :: R1, R2, R3 character(80) :: text character(90) :: line="------------------------------------------------------------------------------------------" !Character formats 1 format (A) 2 format (/A/) 3 format (A/) 4 format (/A) !Combined format 10 format (I8,3ES16.8,2I8) open (unit=1, status="old", file="text.in", action="read") print 4, line print 3, "CASE 1" print 3, "Read numbers from file into int/real variables:" print 1, "Results:" print 1, " I1 R1 R2 R3 I2 I3" read_loop_1: do I1=-99; I2=-99; I3=-99 R1=-99._DBL; R2=-99._DBL; R3=-99._DBL read(1,10,iostat=iost) I1, R1, R2, R3, I2, I3 if ( iost /= 0 ) then if ( iost < 0 ) then print 4, "End Of File Reached" print 3, line else if ( iost > 0 ) then print *, "ERROR reading in CASE 1: iost = ", iost end if exit read_loop_1 else print 10, I1, R1, R2, R3, I2, I3 end if end do read_loop_1 rewind (1) print 4, line print 3, "CASE 2" print 3, "Read numbers from file into string and then read from string into int/real variables:" print 1, "Results:" print 1, " I1 R1 R2 R3 I2 I3" read_loop_2: do I1=-99; I2=-99; I3=-99 R1=-99._DBL; R2=-99._DBL; R3=-99._DBL read(1,1,iostat=iost) text if ( iost /= 0 ) then if ( iost < 0 ) then print 4, "End Of File Reached" print 3, line else if ( iost > 0 ) then print *, "ERROR reading in CASE 2: iost = ", iost end if exit read_loop_2 else read(text,10,iostat=iost) I1, R1, R2, R3, I2, I3 print 10, I1, R1, R2, R3, I2, I3 end if end do read_loop_2 rewind (1) print 4, line print 3, "CASE 3" print 3, "Read numbers from file into string and then read from string into int/real variables:" print 3, "Add condition that if formatted read fails, then do list-directed read using *" print 1, "Results:" print 1, " I1 R1 R2 R3 I2 I3" read_loop_3: do I1=-99; I2=-99; I3=-99 R1=-99._DBL; R2=-99._DBL; R3=-99._DBL read(1,1,iostat=iost) text if ( iost /= 0 ) then if ( iost < 0 ) then print 4, "End Of File Reached" print 3, line else if ( iost > 0 ) then print *, "ERROR reading in CASE 3: iost = ", iost end if exit read_loop_3 else read(text,10,iostat=iost) I1, R1, R2, R3, I2, I3 if ( iost /= 0 ) then read(text,*,iostat=iost) I1, R1, R2, R3, I2, I3 end if print 10, I1, R1, R2, R3, I2, I3 end if end do read_loop_3 print 2, "NORMAL TERMINATION" close (1) end program read_csv