Search code examples
matrixfile-iofortranfortran77

Update a matrix into a text file without appending the results


I have a Fortran 77 code like this, more or less:

nMaxRow=100
nMaxStep=100
! initialization of the matrix if Step=1
do step=1,nMaxStep

   if (step.eq.1) then
      do ii=1,nMaxRow
         do jj=1,nMaxStep
            A(ii,jj)=0
         end do
      end do
   end if


!now for each step and for each row update the cell of the matrix
   do ii=1,nMaxRow
      A(ii,step)=X(ii)  !X(ii) is a number associated with the specific ow at that specific step
   end do

!Now I want to write the updated matrix at this step into a text file,
!How can I do that????

end do   !close the do step... 

Is it possible to update the values of the matrix and write the updated matrix at that specific step into a text file? I mean, without appending the results each step...

I found out that for Fortran 90 the 'REPLACE' command exists... but I couldn't find anything similar for Fortran 77.

One simple idea would be deleting the file just before writing a new one... but I don't like it and I don't know how to do it anyway.


Solution

  • If the file is already open (from the previous writing), you can just go the the start of the file using

      rewind(unitnumber)
    

    and start writing again. It will delete the original content of the file and start again. If you wan't to go back just be several records, you can use backtrace(), but you probably don't want that here.

    If it isn't open, just open it and start writing. Unless you open it of appending, it will overwrite the original content.