How to skip a data line in fortran [duplicate]

回眸只為那壹抹淺笑 提交于 2019-12-13 10:40:14

问题


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


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/34307187/how-to-skip-a-data-line-in-fortran

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!