PROGRAM Accesso_flusso
    IMPLICIT NONE
    INTEGER :: i, j, n, r
    INTEGER, PARAMETER :: Nr=5, Nc=10
    INTEGER, DIMENSION(Nr,Nc) :: mat
    INTEGER, DIMENSION(Nr) :: posiz

    DO i = 1, Nc
       DO j = 1, Nr
          mat(j,i) = (j-1)*Nc + i
       ENDDO
    ENDDO
    WRITE(*,*) "Matrice:"
    DO j = 1, Nr
       WRITE(*,100) (mat(j,i),i=1,Nc)
    ENDDO
    WRITE(*,"(/)")

    OPEN(UNIT = 11, FILE = 'salva.dun', ACCESS='stream', FORM = 'UNFORMATTED', STATUS="REPLACE")
    do j = 1, Nr
       INQUIRE(11,POS=posiz(j))
       WRITE(11,IOSTAT=n) (mat(j,i),i=1,Nc)
       IF (n.NE.0) then
          PRINT*,' Errore scrittura record ',j
       END IF
    ENDDO
    CLOSE(UNIT = 11)

   WRITE(*,*) "Riga da leggere?"
   READ*,r
   IF ( r >=1 .AND. r <= Nr ) THEN
     ! Si azzera la matrice per verifica
      mat(:,:) = 0
      OPEN(UNIT = 22, FILE = 'salva.dun', ACCESS='stream', FORM = 'UNFORMATTED', STATUS="OLD")
      WRITE(*,*) "Riga ",r," in posizione ",posiz(r),":"
      READ(22,IOSTAT=n, POS=posiz(r))  (mat(r,i),i=1,Nc)
      IF (n.NE.0) then
          PRINT*,' Errore lettura record ',r
      END IF
      WRITE(*,100) (mat(r,i),i=1,Nc)
      CLOSE(UNIT = 22)
100 FORMAT(1x,10(i6.2))
   ENDIF
   STOP
END
