2011-03-06 130 views
0

我正在使用Fortran 95編寫代碼來讀取多個類似1.dat,2.dat,......,9999.dat的文件。我有一個讀取0001.dat,0002.dat,.......... 9999.dat的代碼。它看起來像在Fortran 95中讀取多個文件

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') 

你可以建議我怎樣才能使我有可能讀取我有的文件嗎? Thanks.u

回答

2

如果你改變你的格式

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 
+0

謝謝阿蘭。然而,我得到了同樣的錯誤,如:list in:文件末尾明顯狀態:單元1命名爲1.dat最後格式:列表io最近閱讀順序格式化外部IO中止您對此有何看法? – nagendra 2011-03-06 21:54:47

+0

您可以使用這些文件的樣本更新問題,以便我們爲您提供一個簡單的閱讀程序? – 2011-03-06 22:07:36

+0

當然。其實我在Linux [Ubuntu]工作,我自己寫了數據文件。我不知道如何爲您提供我的數據文件,但它包含3列和9行。而已。如果我將文件重命名爲0001.dat,0002.dat而不是1.dat,2.dat,則該程序可以讀取。 – nagendra 2011-03-06 22:46:24