This question already has an answer here:
-
Skip a line from text file in Fortran90
3 answers
enter image description here
I have this data file
and want to read this data without line 1,2,3,4,5
program example
real data(15,9)
OPEN ( unit=10, file='filename' )
do i = 1, 15
READ (10, *) (data(i,j), j=1,10)
enddo
print *, data(4,1), data(4,2), data(4,3)
stop
end
this is my fortran code.
how can i change this code
Looking something like this?
input file: data
line1
line2
line3
line4
line5
line6
line7
line8
line9
line10
fortran code:
implicit none
integer:: lskip,lread
character(len=20)::line
open(20, file = "data")
!skip first 5 line
do lskip = 1,5
read(20,*)
End do
! First 5 lines skiped
! Now read actual lines
do lread = 1,5
read(20,*)LINE
write(*,*)line
End do
close(20)
end
Result
$gfortran so.f90
$./a.out
line6
line7
line8
line9
line10
NB: This is a minimal example, just for showing the skipping. You will change the read inside lread
loop to actually read your file according to your data format
One way to do this is to put in a READ statement for each line that you want to "skip". Each time a READ statement is encountered, it reads in the data and then moves the "pointer" in the file down to the next line. So, for example, to skip 3 lines of header information:
DO 50 ilines = 1,3
READ(1,*)
50 continue
This in effect READs and stores nothing, but moves the pointer in the file forward 3 lines.