1

someone produced an output for me with the following subroutine

subroutine write(...)
REAL*8 v(a), h(b), f(b)
integer iv(b+1), jv(a)

 write(ium) a, b, c, d, e
 write(ium) (iv(i),i=1,b+1)
 write(ium) (jv(i),i=1,a)
 write(ium) (v(i),i=1,a)
 write(ium) (f(i),i=1,b)
 write(ium) (h(i),i=1,b)

return
end

I know that a, b, c, d and e will all be integers (although the routine doesn't specify them as such, is that okay?!). The whole output went into a binary file which he send to me and I now want to retrieve all the (seperated) information back.

I really don't know much about Fortran I/O so I'm hoping someone can help...

1
  • Nope, because the only info I got on how he wrote the file is this routine... Commented Oct 10, 2013 at 11:04

1 Answer 1

1

Here is a simple example, basically just exchange write by read. Take care to allocate the arrays first.

program test
  implicit none

  integer,parameter   :: ium=1234
  integer             :: a=1, b=2, c=3, d=4, e=5
  REAL*8,allocatable  :: v(:), h(:), f(:)
  integer,allocatable :: iv(:), jv(:)
  integer             :: i, stat

  ! Set values
  allocate( v(a), h(b), f(b), iv(b+1), jv(a), stat=stat )
  if (stat/=0) stop 'Cannot allocate memory!'

  v=1.d0
  h=2.d0
  f=3.d0
  iv = 4
  jv = 5

  write(*,*) v, h, f

  ! Write back
  open(unit=ium, file='test',form='unformatted',status='replace',action='write')

  write(ium) a, b, c, d, e
  write(ium) (iv(i),i=1,b+1)
  write(ium) (jv(i),i=1,a)
  write(ium) (v(i),i=1,a)
  write(ium) (f(i),i=1,b)
  write(ium) (h(i),i=1,b)

  close(ium)

  ! Scratch
  deallocate( v, h, f, iv, jv )

  !========================================
  ! Read in
  open(unit=ium, file='test',form='unformatted',status='old',action='read')

  read(ium) a, b, c, d, e
  ! Allocate arrays
  allocate( v(a), h(b), f(b), iv(b+1), jv(a), stat=stat )
  if (stat/=0) stop 'Cannot allocate memory!'

  read(ium) (iv(i),i=1,b+1)
  read(ium) (jv(i),i=1,a)
  read(ium) (v(i),i=1,a)
  read(ium) (f(i),i=1,b)
  read(ium) (h(i),i=1,b)

  close(ium)
  write(*,*) v, h, f

  deallocate( v, h, f, iv, jv )
end program

Adjust this to your needs.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 -- worth a note "unformatted" fortran is not nescesaritly platform portable, so if this fails you may need to dig deeper. (and it is not an advisable way to exchange data )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.