How do I read only every tenth set of lines in a large file? A simple example is shown below. I thought I could use the increment for the do loop, but it seems that only changes the index T below, but does not skip over the desired set of lines.
IMPLICIT NONE
INTEGER :: T
REAL(8) :: RR,RS,RT
OPEN(UNIT=11,FILE='test.dat',STATUS='UNKNOWN',ACTION='READ')
DO T = 1,1000,10
READ(11,*) RR
READ(11,*) RS
READ(11,*) RT
ENDDO
CLOSE(11)
END PROGRAM TRACE
You have written a loop whose skip is independent of the skip over the rows of the input file. You need to connect the two, like the following code,
Pay attention to how reaching the end of the file can be checked via the intrinsic
is_iostat_end()which does not require knowing the number of lines in the file a priori.Also, note the
iso_fortran_envintrinsic module to avoid the use of non-standard, potentially non-portable extensions to the language such asREAL(8). Also, it is good practice not to type modern Fortran syntax in uppercase letters.Here is the sample output:
Test it here.