Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read from an unformatted unknown file

Status
Not open for further replies.

Londoil

Technical User
Dec 12, 2011
2
CA
Hi

I have an unformatted file that was written by a fortran program. The guy that wrote the program can't be reached and the source of the program can't be found.

I know that the file contains 1 integer, 2 real (or double precision ) three-dimensional arrays and 4 real (or double precision) two dimensional arrays. I do not know the sizes of the arrays, I do know that they are of the same size (i.e., the three dimensional arrays have size LxMxN and the two dimensional array - LxM, where L, M and N are not known).

Is there any way to salvage the information? Or we are lost here?
 
With a systematic work, you could find the right combination. But this is not obvious. Few advices for starting :

- an hexadecimal editor could be very useful

- it is rather easy to write a Fortran program able to give you
pieces of information like the number of records (number of executed WRITE statements) and the length of each record

In both cases, this is a first step. After that you have to deduce what could be the contents of each record.

Code:
program analysis
  implicit none
  integer :: i,n,k,ierr
  character :: line(10000000) ! a very long record
  open(15,file="test.bin",form='unformatted')
  n=0
  do
    read(15,end=20)
    n=n+1
  enddo
  20 continue
  write(*,*) 'number or records ',n
  rewind(15)
  do i=1,n
    k=0
    do
      read(15,iostat=ierr) line(1:k)
      if(ierr /= 0) exit
      k=k+1
      backspace(15)
    enddo
    write(*,*) 'length of the recored ',i,' : ',k-1,' bytes'
  enddo
end program

Example of analysis :

Code:
program ex
  implicit none
  open(15,file='test.bin',form='unformatted')
  write(15) 32,64,57
  write(15) 1.d25,-0.327d0
end program ex

Code:
[lcoul@localhost test]$ ifort ex.f90
[lcoul@localhost test]$ ./a.out
[lcoul@localhost test]$ ifort analysis.f90
[lcoul@localhost test]$ ./a.out
 number or records            2
 length of the recored            1  :           12  bytes
 length of the recored            2  :           16  bytes

François Jacq
 
Wow! Thanks for the effort. I understand the idea, will try to implement it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top