简体   繁体   English

从Fortran中的特定行号开始读取

[英]Starting reading from specific line numbers in Fortran

I have a file with 1000s of numbers like: 我有一个包含1000的数字的文件,例如:

0000
0032
1201
:  :
:  :
:  :
2324

Depending on an input parameter "n", I want to read "m" numbers from this file from line numbers "n" to "n+m-1". 根据输入参数“ n”,我想从该文件中从行号“ n”到“ n + m-1”读取“ m”个数字。

Any ideas how can I do this in Fortran? 有什么想法可以在Fortran中做到吗?

I don't know if you have tried it yourself, but here is an minimal example: say, your input file looks like this: 我不知道您是否亲自尝试过,但这是一个最小的示例:例如,您的输入文件如下所示:

0000
0032
1201
1234
4567
7890
2324

use this code (after reading it) 使用此代码(阅读后)

Program jhp
Implicit None
integer :: i
integer, parameter :: &
     m=7, &    !total number of line
     n=4, &    !line to skip
     p=3      !lines to read
integer,dimension(m)::arr   !file to read

open(12,file='file_so',status='old')
do i=1,n
  read(12,*)arr(i)
end do
do i=1,p
  read(12,*)arr(i)
  write(*,*)arr(i)
end do
End Program jhp

This skips first n line, and reads p lines after that. 这将跳过前n行,然后再读取p行。 Hope that helps 希望能有所帮助

may be, 也许,

open (unit, file ...)
do i=1,n
 read(unit,*) crap
end do

do i =n,n+m-1
 read(unit,*) whatever
end do
close(unit)

is what you are looking for. 是您要寻找的。 this is untasted, but may give you a go. 这是未尝的,但可能会给您带来帮助。

edit: direct access is better for this type of job: Just realised, though this is the easiest one, not the preferred one. 编辑:对于这种类型的工作,直接访问更好:刚刚实现,尽管这是最简单的,而不是首选。 You can open the file in direct access mode and complete your job as: 您可以在直接访问模式下打开文件,并按以下步骤完成工作:

OPEN( unit, file, ACCESS='DIRECT', RECL=100, FORM='FORMATTED')

READ( unit, *, REC=n, ERR=10 ) x

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM