Read/write binary files in Linux and Windows



Dear Fortran experts,

I am using the following code to read and write binary files in both a Linux cluster and in Windows PC.

OS/COMPILERS
In Windows: CVF 6.6 (little Endian)
In Linux: PGI (Portland Group) and Sun compilers (little Endian)

PROBLEM:
The code shown below was compiled in both OS systems. NO error was reported. I have retrieved the same numbers saved in the array x, in each system independently. The problem is with the size of the files:

in Windows: an array x(5,5,2) in real(4) is 800 bytes
in Linux: an array x(5,5,2) real(4) is 200 bytes

In my opinion Linux is doing the right thing with respect to the size. The Windows binary file has trailing zeros.

Does someone know why this happen? What can I do to solve this problem? My intention is to use the same binary files (vary many...) in both systems since I know that both systems are little Endian. At the moment I can't do it.

Thank you in advance for your help!

Luis


>>> CODE:

Program binary
! ... some stuff
call writeBin
call readBin
! ...
Program binary

subroutine writeBin
integer(4) :: i, j, d
integer(4), parameter :: m=5, n=5, t=2
real(4) :: x(m,n,t)
!...
!insert some code to fill x
!...
!open direct access unformatted file with
! records sized for the whole array
open (unit=1, &
file='out.r4', &
form='unformatted', &
access='direct', &
recl=m*n*4)
!write the whole thing as one record
do d=1,t
write (1,rec=d) x(:,;,d)
end do
! ...
end subroutine writeBin


subroutine readBin is basically the same but with a read statement.







--
Dr. Luis Samaniego
Department Computational Hydrosystems (CHS)
Helmholtz Centre for Environmental Research - UFZ
Permoserstraße 15 / 04318 Leipzig / Germany

luis.samaniego@xxxxxx / http://www.ufz.de
phone +49 341 235 3971 / fax 49 341 235 3939

+++++++++++++++++++++++++++++++++++++++++++++++++

The UFZ has a new name:
Helmholtz Centre for Environmental Research - UFZ

+++++++++++++++++++++++++++++++++++++++++++++++++

Helmholtz-Zentrum für Umweltforschung GmbH - UFZ
Registered Office: Leipzig
Registration Office: Amtsgericht Leipzig
Trade Register Nr. B 4703
Chairman of the Supervisory Board: MinDirig Hartmut F. Grübel
Scientific Director: Prof. Dr. Georg Teutsch
Administrative Director: Dr. Andreas Schmidt
.



Relevant Pages