在 Fortran 95 中读取多个文件

发布于 2024-10-20 14:10:20 字数 331 浏览 3 评论 0原文

我正在用 Fortran 95 编写代码,以读取许多类似于 1.dat、2.dat、......、9999.dat 的文件。我有一个代码,读取 0001.dat、0002.dat、....... 9999.dat。看起来

character*12, fn
..
DO i=1,n
    write(fn,*)i
    open(1,file=fn)
    do j=1,5
       read(1,*)x(i,j),y(i,j),z(i,j)
        end do
 10  format(i4.4,'.dat')

您可以建议我如何才能读取我拥有的文件吗?谢谢你

I am writing codes in Fortran 95, to read a number of files looking like 1.dat, 2.dat, ......, 9999.dat. I have a code that reads 0001.dat, 0002.dat, .......... 9999.dat. It looks like

character*12, fn
..
DO i=1,n
    write(fn,*)i
    open(1,file=fn)
    do j=1,5
       read(1,*)x(i,j),y(i,j),z(i,j)
        end do
 10  format(i4.4,'.dat')

May you suggest me how can I make it possible to read the files that I have please? Thanks.u

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

反话 2024-10-27 14:10:20

如果你将格式更改为

10    format (i4,'.dat')

我尝试过的格式,例如

      character *8 filename

      do i=1,9999
        write (filename, 10) i  
        write (*,*) filename
      end do

10    format (i4,'.dat')

      end

,我的文件名中没有前导零,该怎么办?

这顶帽子是您要找的吗?

更新
我明白了...

那么格式应该是

10    format (i0,'.dat')

“0”表示左对齐。

我用以下 pgm 测试了它

    character*12, fn

    integer x(11,5)
    integer y(11,5)
    integer z(11,5)

    do i=1,11
        write(fn,10)i
        open(1,file=fn)
        do j=1,5
            read(1,*)x(i,j),y(i,j),z(i,j)
        end do
        close(1)
    end do

10  format(i0,'.dat')

    end

,这对我有用。

更新2

    implicit none

    integer n,ns,i,j

    real x(9999,400),y(9999,400),z(9999,400),a(9999,400),aa(400)

    character*12, fn

        n =  14
    ns = 2  

    do i = 0,n
        do j = 1, 5
                a(i,j) = 0.0
            end do
    end do

    do i=1,n               
        write(fn,10)i
        open(1,file=fn)

        do j=1,5
                read(1,*)x(i,j),y(i,j),z(i,j)
                if(i.le.ns) then
                     a(i,j) = x(i,j)
            else
                 a(i,j) = x(i,j) + a(i-ns,j)
                end if
            aa(j) = a(i,j)
            write(*,*) j,x(i,j),y(i,j),z(i,j),a(i,j)
        end do
        close(1)

        do j = 1, 5
            write(*,*) aa(j)
        end do
    end do

10  format(i0,'.dat')       

    end

对我有用。

输出(在 Ubuntu 10.10 上)。

./a.out
           1   1.0000000       3.0000000       5.0000000       1.0000000    
           2   2.0000000       5.0000000       7.0000000       2.0000000    
           3   3.0000000       7.0000000       8.0000000       3.0000000    
           4   1.0000000       4.0000000       8.0000000       1.0000000    
           5   1.0000000       4.0000000       8.0000000       1.0000000    
   1.0000000    
   2.0000000    
   3.0000000    
   1.0000000    
   1.0000000    
           1   3.0000000       8.0000000       5.0000000       3.0000000    
           2   7.0000000       5.0000000       7.0000000       7.0000000    
           3   3.0000000       7.0000000       8.0000000       3.0000000    
           4   1.0000000       4.0000000       10.000000       1.0000000    
           5   3.0000000       5.0000000       7.0000000       3.0000000    
   3.0000000    
   7.0000000    
   3.0000000    
   1.0000000    
   3.0000000    
           1   3.0000000       8.0000000       5.0000000       4.0000000    
           2   7.0000000       5.0000000       7.0000000       9.0000000    
           3   3.0000000       7.0000000       8.0000000       6.0000000    
           4   1.0000000       4.0000000       10.000000       2.0000000    
           5   3.0000000       5.0000000       7.0000000       4.0000000    
   4.0000000    
   9.0000000    
   6.0000000    
   2.0000000    
   4.0000000    
           1   3.0000000       8.0000000       5.0000000       6.0000000    
           2   7.0000000       5.0000000       7.0000000       14.000000    
           3   3.0000000       7.0000000       8.0000000       6.0000000    
           4   1.0000000       4.0000000       10.000000       2.0000000    
           5   3.0000000       5.0000000       7.0000000       6.0000000    
   6.0000000    
   14.000000    
   6.0000000    
   2.0000000    
   6.0000000    
           1   3.0000000       8.0000000       5.0000000       7.0000000    
           2   7.0000000       5.0000000       7.0000000       16.000000    
           3   3.0000000       7.0000000       8.0000000       9.0000000    
           4   1.0000000       4.0000000       10.000000       3.0000000    
           5   3.0000000       5.0000000       7.0000000       7.0000000    
   7.0000000    
   16.000000    
   9.0000000    
   3.0000000    
   7.0000000    
           1   3.0000000       8.0000000       5.0000000       9.0000000    
           2   7.0000000       5.0000000       7.0000000       21.000000    
           3   3.0000000       7.0000000       8.0000000       9.0000000    
           4   1.0000000       4.0000000       10.000000       3.0000000    
           5   3.0000000       5.0000000       7.0000000       9.0000000    
   9.0000000    
   21.000000    
   9.0000000    
   3.0000000    
   9.0000000    
           1   3.0000000       8.0000000       5.0000000       10.000000    
           2   7.0000000       5.0000000       7.0000000       23.000000    
           3   3.0000000       7.0000000       8.0000000       12.000000    
           4   1.0000000       4.0000000       10.000000       4.0000000    
           5   3.0000000       5.0000000       7.0000000       10.000000    
   10.000000    
   23.000000    
   12.000000    
   4.0000000    
   10.000000    
           1   3.0000000       8.0000000       5.0000000       12.000000    
           2   7.0000000       5.0000000       7.0000000       28.000000    
           3   3.0000000       7.0000000       8.0000000       12.000000    
           4   1.0000000       4.0000000       10.000000       4.0000000    
           5   3.0000000       5.0000000       7.0000000       12.000000    
   12.000000    
   28.000000    
   12.000000    
   4.0000000    
   12.000000    
           1   3.0000000       8.0000000       5.0000000       13.000000    
           2   7.0000000       5.0000000       7.0000000       30.000000    
           3   3.0000000       7.0000000       8.0000000       15.000000    
           4   1.0000000       4.0000000       10.000000       5.0000000    
           5   3.0000000       5.0000000       7.0000000       13.000000    
   13.000000    
   30.000000    
   15.000000    
   5.0000000    
   13.000000    
           1   3.0000000       8.0000000       5.0000000       15.000000    
           2   7.0000000       5.0000000       7.0000000       35.000000    
           3   3.0000000       7.0000000       8.0000000       15.000000    
           4   1.0000000       4.0000000       10.000000       5.0000000    
           5   3.0000000       5.0000000       7.0000000       15.000000    
   15.000000    
   35.000000    
   15.000000    
   5.0000000    
   15.000000    
           1   3.0000000       8.0000000       5.0000000       16.000000    
           2   7.0000000       5.0000000       7.0000000       37.000000    
           3   3.0000000       7.0000000       8.0000000       18.000000    
           4   1.0000000       4.0000000       10.000000       6.0000000    
           5   3.0000000       5.0000000       7.0000000       16.000000    
   16.000000    
   37.000000    
   18.000000    
   6.0000000    
   16.000000    
At line 23 of file c.for (unit = 1, file = '12.dat')
Fortran runtime error: End of file

What if you change your format to

10    format (i4,'.dat')

I tried this for instance

      character *8 filename

      do i=1,9999
        write (filename, 10) i  
        write (*,*) filename
      end do

10    format (i4,'.dat')

      end

And I had no leading zeros in my file names.

Is this hat you were looking for ?

Update
I see...

Then the format should be

10    format (i0,'.dat')

the '0' means left justified.

I tested it with the following pgm

    character*12, fn

    integer x(11,5)
    integer y(11,5)
    integer z(11,5)

    do i=1,11
        write(fn,10)i
        open(1,file=fn)
        do j=1,5
            read(1,*)x(i,j),y(i,j),z(i,j)
        end do
        close(1)
    end do

10  format(i0,'.dat')

    end

And that worked for me.

Update 2

    implicit none

    integer n,ns,i,j

    real x(9999,400),y(9999,400),z(9999,400),a(9999,400),aa(400)

    character*12, fn

        n =  14
    ns = 2  

    do i = 0,n
        do j = 1, 5
                a(i,j) = 0.0
            end do
    end do

    do i=1,n               
        write(fn,10)i
        open(1,file=fn)

        do j=1,5
                read(1,*)x(i,j),y(i,j),z(i,j)
                if(i.le.ns) then
                     a(i,j) = x(i,j)
            else
                 a(i,j) = x(i,j) + a(i-ns,j)
                end if
            aa(j) = a(i,j)
            write(*,*) j,x(i,j),y(i,j),z(i,j),a(i,j)
        end do
        close(1)

        do j = 1, 5
            write(*,*) aa(j)
        end do
    end do

10  format(i0,'.dat')       

    end

Worked for me.

Output (on Ubuntu 10.10).

./a.out
           1   1.0000000       3.0000000       5.0000000       1.0000000    
           2   2.0000000       5.0000000       7.0000000       2.0000000    
           3   3.0000000       7.0000000       8.0000000       3.0000000    
           4   1.0000000       4.0000000       8.0000000       1.0000000    
           5   1.0000000       4.0000000       8.0000000       1.0000000    
   1.0000000    
   2.0000000    
   3.0000000    
   1.0000000    
   1.0000000    
           1   3.0000000       8.0000000       5.0000000       3.0000000    
           2   7.0000000       5.0000000       7.0000000       7.0000000    
           3   3.0000000       7.0000000       8.0000000       3.0000000    
           4   1.0000000       4.0000000       10.000000       1.0000000    
           5   3.0000000       5.0000000       7.0000000       3.0000000    
   3.0000000    
   7.0000000    
   3.0000000    
   1.0000000    
   3.0000000    
           1   3.0000000       8.0000000       5.0000000       4.0000000    
           2   7.0000000       5.0000000       7.0000000       9.0000000    
           3   3.0000000       7.0000000       8.0000000       6.0000000    
           4   1.0000000       4.0000000       10.000000       2.0000000    
           5   3.0000000       5.0000000       7.0000000       4.0000000    
   4.0000000    
   9.0000000    
   6.0000000    
   2.0000000    
   4.0000000    
           1   3.0000000       8.0000000       5.0000000       6.0000000    
           2   7.0000000       5.0000000       7.0000000       14.000000    
           3   3.0000000       7.0000000       8.0000000       6.0000000    
           4   1.0000000       4.0000000       10.000000       2.0000000    
           5   3.0000000       5.0000000       7.0000000       6.0000000    
   6.0000000    
   14.000000    
   6.0000000    
   2.0000000    
   6.0000000    
           1   3.0000000       8.0000000       5.0000000       7.0000000    
           2   7.0000000       5.0000000       7.0000000       16.000000    
           3   3.0000000       7.0000000       8.0000000       9.0000000    
           4   1.0000000       4.0000000       10.000000       3.0000000    
           5   3.0000000       5.0000000       7.0000000       7.0000000    
   7.0000000    
   16.000000    
   9.0000000    
   3.0000000    
   7.0000000    
           1   3.0000000       8.0000000       5.0000000       9.0000000    
           2   7.0000000       5.0000000       7.0000000       21.000000    
           3   3.0000000       7.0000000       8.0000000       9.0000000    
           4   1.0000000       4.0000000       10.000000       3.0000000    
           5   3.0000000       5.0000000       7.0000000       9.0000000    
   9.0000000    
   21.000000    
   9.0000000    
   3.0000000    
   9.0000000    
           1   3.0000000       8.0000000       5.0000000       10.000000    
           2   7.0000000       5.0000000       7.0000000       23.000000    
           3   3.0000000       7.0000000       8.0000000       12.000000    
           4   1.0000000       4.0000000       10.000000       4.0000000    
           5   3.0000000       5.0000000       7.0000000       10.000000    
   10.000000    
   23.000000    
   12.000000    
   4.0000000    
   10.000000    
           1   3.0000000       8.0000000       5.0000000       12.000000    
           2   7.0000000       5.0000000       7.0000000       28.000000    
           3   3.0000000       7.0000000       8.0000000       12.000000    
           4   1.0000000       4.0000000       10.000000       4.0000000    
           5   3.0000000       5.0000000       7.0000000       12.000000    
   12.000000    
   28.000000    
   12.000000    
   4.0000000    
   12.000000    
           1   3.0000000       8.0000000       5.0000000       13.000000    
           2   7.0000000       5.0000000       7.0000000       30.000000    
           3   3.0000000       7.0000000       8.0000000       15.000000    
           4   1.0000000       4.0000000       10.000000       5.0000000    
           5   3.0000000       5.0000000       7.0000000       13.000000    
   13.000000    
   30.000000    
   15.000000    
   5.0000000    
   13.000000    
           1   3.0000000       8.0000000       5.0000000       15.000000    
           2   7.0000000       5.0000000       7.0000000       35.000000    
           3   3.0000000       7.0000000       8.0000000       15.000000    
           4   1.0000000       4.0000000       10.000000       5.0000000    
           5   3.0000000       5.0000000       7.0000000       15.000000    
   15.000000    
   35.000000    
   15.000000    
   5.0000000    
   15.000000    
           1   3.0000000       8.0000000       5.0000000       16.000000    
           2   7.0000000       5.0000000       7.0000000       37.000000    
           3   3.0000000       7.0000000       8.0000000       18.000000    
           4   1.0000000       4.0000000       10.000000       6.0000000    
           5   3.0000000       5.0000000       7.0000000       16.000000    
   16.000000    
   37.000000    
   18.000000    
   6.0000000    
   16.000000    
At line 23 of file c.for (unit = 1, file = '12.dat')
Fortran runtime error: End of file
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文