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