Fortran Coder

标题: 批处理筛选文件 [打印本页]

作者: hpa    时间: 2020-2-17 11:27
标题: 批处理筛选文件
刚开始学习Fortran的小白,因为毕业设计写了一段代码,知道里面肯定有很低级的错误,最后出现end of life 的错误,希望高手们帮忙看一下,帮我指出一下错误
[Fortran] 纯文本查看 复制代码
program test
  integer i,o
  integer long
  integer lat
  real s,a
  real c(15,13028),d
  character(512)::filename
  
  call system('dir *.txt /b >1.dat')
  s=0.0
  open(10,file='1.dat')
  do
    read(10,'(a)',iostat=i) filename
     open(2,file=filename)
     do o = 1,13028   !将文件中数据转化为二维矩阵
     read(2,*)d,c(:,o)
     end do
     close(2)
   long = c(1,3)
   lat =c(1,4)
    if (2600<long .and. long<3947 .and. 7319<lat .and. lat<10447) then
    open(unit=3,file ='test.out') !根据数据中的某一个数据筛选文件
     write(3,*)filename !将符合条件的文件名称放入指定文件夹中
     close(3)
     end if
    if(i/=0) exit
    open(11,file=filename) !获得下一个文件的名称
    read(11,*) a
    close(11)
    s = s + a
  end do
  close(10,status='delete')
  read(*,*)
end program

作者: fcode    时间: 2020-2-17 13:24
你这些 txt 文件是什么内容?格式如何?给一个范例。
if (2600<long .and. long<3947 .and. 7319<lat .and. lat<10447) then
这个判断,是只需要判断每个文件的第一行吗?还是每一行都需要判断?
作者: hpa    时间: 2020-2-17 18:19
fcode 发表于 2020-2-17 13:24
你这些 txt 文件是什么内容?格式如何?给一个范例。
if (2600

您好,txt文件的格式是这样的,就是我上面不是把每一个文件的所有数据转换为了数组,取出这个数组的随便一行的第三个与第四个数,即精度和纬度,并进行判断
C:\Users\alienware\Desktop
作者: hpa    时间: 2020-2-17 18:20
fcode 发表于 2020-2-17 13:24
你这些 txt 文件是什么内容?格式如何?给一个范例。
if (2600

50246        5219        12443        103500        1960        12        1        32766        -327        -250        -391        76        0        13        47
一共是13028行,15列的数据
作者: hpa    时间: 2020-2-17 18:21
fcode 发表于 2020-2-17 13:24
你这些 txt 文件是什么内容?格式如何?给一个范例。
if (2600

数据间的要素是用tab作为的间隔符
作者: fcode    时间: 2020-2-17 21:47
这个判断,是只需要判断每个文件的第一行吗?还是每一行都需要判断?
如果一个文件里,第一行满足,但第二行不满足,第三行又满足,应该如何处理?
作者: hpa    时间: 2020-2-17 22:07
fcode 发表于 2020-2-17 21:47
这个判断,是只需要判断每个文件的第一行吗?还是每一行都需要判断?
如果一个文件里,第一行满足,但第二 ...

麻烦您了,他每一行的这两个数他都是一样的,因为这个文件是一个气象台站的气象资料,这两个数是他的经度和纬度所以我要筛选符合上面要求的地点,所以只需要判断每个文件的第一行的这两个数就行。
作者: fcode    时间: 2020-2-18 20:06
试试这样,如果还有问题,给一个实际的 txt 文件的真实范例。

[Fortran] 纯文本查看 复制代码
program test
  implicit none !//必须写
  integer i,long,lat
  real c(15)
  character(512)::filename
  call system('dir *.txt /b >1.dat')
  open(10,file='1.dat')
  open(3,file ='test.out') !根据数据中的某一个数据筛选文件
  do
    read(10,'(a512)',iostat=i) filename
    if(i/=0) exit
    open(2,file=filename)
    read(2,*) c
    close(2)
    long = c(3)
    lat =c(4)
    if (2600<long .and. long<3947 .and. 7319<lat .and. lat<10447) then
      write(3,*)filename !将符合条件的文件名称放入指定文件夹中
    end if
  end do
  close(3)
  close(10,status='delete')
  read(*,*)
end program test

作者: hpa    时间: 2020-2-21 14:52
fcode 发表于 2020-2-18 20:06
试试这样,如果还有问题,给一个实际的 txt 文件的真实范例。

[mw_shl_code=fortran,true]program test

是这样的,谢谢,麻烦您了




欢迎光临 Fortran Coder (http://bbs.fcode.cn/) Powered by Discuz! X3.2