Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Io Fortran文件I/O二维数组_Io_Fortran - Fatal编程技术网

Io Fortran文件I/O二维数组

Io Fortran文件I/O二维数组,io,fortran,Io,Fortran,要读入程序的文件是一个.txt,称之为numbers.txt,其格式为 75694 13265 98654 我想把每一个数字读入一个秩为2的数组,所以数组的形状是[35]。我的方法的问题似乎正在发展到一个新的记录,我对隐含的DOs没有很好的理解: program f3 implicit none integer, dimension(3,5) :: arr integer :: i, j open(unit=15,file="numbers.txt") ! Only at

要读入程序的文件是一个.txt,称之为numbers.txt,其格式为

75694
13265
98654
我想把每一个数字读入一个秩为2的数组,所以数组的形状是[35]。我的方法的问题似乎正在发展到一个新的记录,我对隐含的DOs没有很好的理解:

program f3
  implicit none
  integer, dimension(3,5) :: arr
  integer :: i, j
  open(unit=15,file="numbers.txt")

  ! Only attempting one method at a time, so one will be commented.                                                                              

  !----- Method 1 - how to advance to next record? -----                                                                                         
  do i=1,3
     do j=1,5
        read(unit=15, fmt='(I1)', advance="no") arr(i,j)
     enddo
  enddo


  !----- Method 2 - get "end of file" error -----                                                                                                
  do i=1,3
     read(unit=15, fmt='(I1)', advance="no") (arr(i,j), j=1,5)
  enddo

  close(15)

  ! Best way to display 2D array?                                                                                                                 
  write(6,'(5I1)') ((arr(i,j), j=1,5), i=1,3)

end program f3
我希望能够使用do循环、隐含do循环或它们的组合,只是为了更好地了解它们的操作,但如果有一种标准方法可以做到这一点,我想知道。谢谢

试着更换这个

  do i=1,3
     do j=1,5
        read(unit=15, fmt='(I1)', advance="no") arr(i,j)
     enddo
  enddo
用这个

  do i=1,3
        read(unit=15, fmt='(5I1)') arr(i,:)
  enddo
那会让你走的

至于写作:

  do ix = 1,3
     write(*,'(5(i1,1x))') arr(ix,:)
  end do

是我可能会采取的一种方法。

完全是个人的和离题的,但我通常选择51x,i1,因为我讨厌尾随空格。@steabert或5I1,:,1X。@francescalus“:”做什么?@KyleG这是冒号编辑描述符。简而言之,在本例中,当在最后一个1X之前没有更多项目要输出时,它终止输出。然而,对于这个答案来说,这是一个太过复杂的问题,甚至对于像这样的简单案例来说,这仅仅是5I2