문제

Code:

program a
implicit double precision (a-h,o-z)
dimension a(3,3),b(3)

open(1,file='matrix.dat')
do i=1,3
    read(1,*) b(i)(a(i,j),j=1,3)
enddo
close(1)

stop
end

Error:

At line 7 of file ea.for (unit=1, file='matrix.dat')
Fortran runtime error: End of file

matrix a(3*3), b(3):

3
2.d0   -7.d0    4.d0    4.d0
1.d0    9.d0   -6.d0    4.d0
-3.d0    8.d0    5.d0    2.d0
도움이 되었습니까?

해결책

Do not use 1 as unit number - try something like 1234.! The lower unit numbers are reserved for "special units" like STDOUT, STDERR, STDIN. See also this post: segmentation error in linux for ansys

To check whether you are trying to read beyond the end of the file or the wrong number of columns you could put iostat=ierror into your read statement to check whether an error occured while reading in. ierror<0 corresponds to "end of file", while ierror>0 means that an error occured during read.

If what you have given for the matrix values corresponds to the file matrix.dat, then you are not reading in the first (integer) value 3.

This is for illustration and should work:

program a_test
  implicit none
  real    :: a(3,3),b(3)
  integer :: dummy, ierror, i, j

  open(unit=1234,file='matrix.dat')
  read(1234,*) dummy
  do i=1,3
      read(1234,*,iostat=ierror) b(i),(a(i,j),j=1,3)
      if (ierror>0) then
        stop 'Error while reading from file. '
      elseif (ierror<0) then
        stop 'Reached end of file. '
      endif
  enddo
  close(1234)

  write(*,*) 'a='
  write(*,*) a(1,:)
  write(*,*) a(2,:)
  write(*,*) a(3,:)
  write(*,*) 'b=',b
end

다른 팁

Read-commands in Fortran move the active point in the file to the next line so that the next read-command can read the next line. My best guess is that the last call to "read" is looking for an endline character, trying to move on to the next line, but running into the end of the file instead. See if adding a hard return at the end of the input file fixes the problem. (I know some older editors complain if you don't have a blank line at the end of a file, and I think this is why.)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top