首页 文章

如何从多个文件中读取数据并写入单个文件的列

提问于
浏览
0

我在Fortran中是全新的,我需要编写一些相对简单的代码 .

我有一些文件(各种文件,例如200个文件);特定节点的每个文件,通过一些简化,每个文件包含:

T(i), X(i)

这些是我的输入,我希望有一个输出文件包含:

T(i) X(i)1 X(i)2 ... X(i)n

问题是我无法将输出文件的不同列中的数据分开,它们在1列中相继出现 .

我的代码是:

PROGRAM Output

implicit none
integer ::nn,n,i,j,l
real,dimension(:),allocatable::t,x,y,z
character(len=10)::TD 

open(11,file='outputX.txt')
allocate (t(1000),x(1000),y(1000),z(1000)) 

n=5                  ! Number of Files
nn=50                ! Number of Rows in each File
 Do i=1,n            ! loop for opening different files 
   write(TD,10)i 
   write(*,*)TD 
   open(1,file=TD) 

      Do l=1,nn      ! loop for reading rows in each file
        read(1,*)t(j),x(j)
        write(11,*)x(j) !!!! This is my PROBLEM, all the data shows in
                               ! one column, I want each file in separately
      Enddo

 Enddo

    10  format('100',i3.3,'') 

    deallocate(x,y,z,t) 
    END PROGRAM Output

我得到的输出是这样的:

11
12
13
21
22
23
31
32
33

但实际上我想:

11    21   31
12    22   32
13    23   33

3 回答

  • 0

    您的代码有几个问题

    Do i=1,n            ! loop for opening different files 
       write(TD,10)i 
       write(*,*)TD 
       open(1,file=TD) 
       Do l=1,nn      ! loop for reading rows in each file
         read(1,*)t(j),x(j)
         write(11,*)x(j) !!!! This is my PROBLEM, all the data shows in
                                   ! one column, I want each file in separately
      End do
    End do
    

    索引 j 完全未定义 . 你应该在循环中的某处放置 j=10j=j+1 .

    另一个问题是你的输出 . 您正在按顺序读取文件 . 将每个文件打印到单独的列中非常困难 . 每个文件的单独行很容易:

    write(11,*) x(1:nn)
    

    after 内循环 .

    或者更精细的控制并避免换行

    write(11,'999(g0,1x)') x(1:nn)
    

    (g0是一般编辑描述符,仅使用必要的宽度) . 这只有在你修复我上面提到的 j 问题时才有效!

    要将它放入单独的列中,您必须这样做

    • 同时打开所有文件,然后从每个文件中读取并在单个写入命令中打印读取的数据 .

    要么

    • 将所有文件中的所有数据存储到2D阵列中的单独列中,然后打印2D阵列 .
  • 0

    所以我得到的是 x(1) belongs to 11x(2) belongs to 12x(3) belongs to 13 等等,不是吗?你可以尝试这个:

    write(11,100) x(j), x(j+3), x(j+6)
    100 format(1X,11F20.4,11F20.4,11F20.4)
    
  • 2

    2D阵列...对于这个问题它是向后但它应该给你一些帮助...(希望)

    PROGRAM Output
    IMPLICIT NONE
    INTEGER, PARAMETER              :: nfiles = 5
    INTEGER, PARAMETER              :: nrows  = 50
    integer                         :: iFile, iRow   !File and row counter/#
    real,dimension(:,:),allocatable :: t,x,y,z       !The are now 2D
    INTEGER,dimension(nFiles)       :: lFile         !logical file indexed by file#
    LOGICAL,dimension(nFiles)       :: Open4Biz      !logical for closing
    character(len=10)::TD                            !Unsure about this
    
    open(11,file='outputX.txt')
    allocate (t(iRow,iFile),x(iRow,iFile),y(iRow,iFile),z(iRow,iFile)) 
    
    Open4Biz(:) = .FALSE.                            !Initialize
    
    !n=5                  ! Number of Files              ^^Moved up/renamed^^
    !nn=50                ! Number of Rows in each File  ^^Moved up/renamed^^
     Files_Loop1: Do iFile = 1, nFiles     !I think that the loop identifier is std f95 (std ifort anyhow)
       write(TD,10) iFile          !Unsure about this
       write(*,*)TD 
       lFile(iFile) = 10+iFile
       open(lFile(iFile),file=TD)  !Probably put in some logic if the file is not found
       Open4Biz(iFile) = .TRUE.
     ENDDO Files_Loop1
    
     Rows_Loop: Do iRow = 1, nn                !The loops are backwards from normal
       Files_Loop2: Do iFile = 1, nFiles
         read(lFile(iFile),*) t(iRow, iFile), x(iRow, iFile)
       Enddo Files_Loop2
       write(11,*) x(iRow,:)
     Enddo Rows_Loop
    
    10  format('100',i3.3,'')
    
    667  CONTINUE                         !This is label to 'jump to' from a bad open
    Files_Loop3: Do iFile = 1, nFiles
      IF(Open4Biz(iFile) CLOSE(lFile(iFile))
    ENDDO Files_Open_Loop
    
    IF(ALLOCATED(X)) deallocate(x) 
    IF(ALLOCATED(Y)) deallocate(y) 
    IF(ALLOCATED(Z)) deallocate(z) 
    IF(ALLOCATED(T)) deallocate(t)
    END PROGRAM Output
    

相关问题