当前位置: 动力学知识库 > 问答 > 编程问答 >

Reading multiple data files in a single array in Fortran

问题描述:

I am using fortran 77. I have multiple (say N) data files containing data of same size (say S) each. I want to read all these data files in one single array of size (N*S) into my program. The data files are named in a arranged manner (datafile1.dat, datafile2.dat......datafileN.dat).

Any clues how can I do it?

Thank you!

网友答案:

Using Fortran 95 notation a possible solution could look like:

program test
  implicit none

  integer, parameter :: NN = 2
  integer, parameter :: SS = 3
  character(len=128) :: fname
  integer :: data(NN * SS)
  integer :: ii

  do ii = 1, NN
    write(fname, "(A,I0,A)") "datafile", ii, ".dat"
    open(12, file=fname, form="formatted")
    read(12, *) data((ii-1)*SS+1:ii*SS)
    close(12)
  end do
  print *, data

end program test

You can easily rewrite it into Fortran 77 notation if needed although most compilers understand Fortran 90/95 already:

      program test
      implicit none

      integer NN, SS
      parameter(NN = 2, SS = 3)
      character*128 fname
      integer data(NN * SS)
      integer ii, jj

      do ii = 1, NN
        write(fname, "(A,I0,A)") "datafile", ii, ".dat"
        open(12, file=fname, form="formatted")
        do jj = 1, SS
          read(12, *) data((ii-1)*SS+jj)
        end do
        close(12)
      end do
      print *, data

      end program test
分享给朋友:
您可能感兴趣的文章:
随机阅读: