How to overwrite part of a binary file?

  Kiến thức lập trình

I want to process a binary file and write the results into a new one of the same format.

My code starts by copying the header, then, for each data frame: load, process, and output it:

      program main
      use, intrinsic :: iso_fortran_env, only: int32, real32
      implicit none

      integer(kind=int32),dimension(:),allocatable :: header
      real(kind=real32),dimension(:),allocatable :: frame

      integer(kind=int32) nb_frames, frame_sz
      integer i

      allocate(header(256))

      open(unit=1,file='fort.1',access='stream',status='old')
      read(2) header

      frame_sz = header(1)
      nb_frames = header(2)
      allocate(frame(frame_sz))

      open(unit=2,file='fort.2',access='stream',status='new')
      write(2) header

      do i=1,nb_frames
          read(1) frame
C ...     do some computations
          write(2) frame
      enddo

      close(unit=1)

C ... PLACEHOLDER: update the header in the output file (unit #2)

      close(unit=2)
      deallocate(header)
      deallocate(frame)

      end

After this processing, I need to update the header in the outputted file. How would you do it?

LEAVE A COMMENT