Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28591 Discussions

Can a binary file created by ifort read in g77 ?

eigo
Beginner
1,291 Views
Hi,

I want to know how to read a binary file created by ifort in g77 compilar.

I compiled the following code with ifort, e.g.
pi = ATAN(1.d0)*4.d0
pi2 = 3.141592653589793d0
OPEN(10,FILE='rfile',FORM='UNFORMATTED')
WRITE(10) pi, pi2
CLOSE(10)
without options, and after creating 'rfile' I try to read 'rfile' with g77 as
OPEN(10,FILE='rfile',FORM='UNFORMATTED')
READ(10) pi, pi2
CLOSE(10)
WRITE(*,*) pi, pi2
but the output result is wrong,
3.14159274 1.12103877E-44

Is some paticular options needed in ifort ?
0 Kudos
7 Replies
Steven_L_Intel1
Employee
1,291 Views
Are you using the x64 version of g77? If so, the answer is "no". Please use gfortran instead (or use ifort!)

32-bit g77 should be the same as ifort for this program.
0 Kudos
mecej4
Honored Contributor III
1,291 Views
The second number output, 1.12103877E-44, if single precision (4-byte), has the internal representation '00000008'. A record-length marker was read as data, which implies that there is a mismatch between writer and reader programs.
0 Kudos
Steven_L_Intel1
Employee
1,291 Views
This very strongly suggests that it is indeed a 64-bit g77 which always uses 8-byte record lengths. Intel Fortran doesn't do this and neither does gfortran nowadays. (Who still uses g77???)
0 Kudos
eigo
Beginner
1,291 Views
Hi, steve

I am using 64-bit g77, which is gcc version 3.4.6 20060404 (Red Hat 3.4.6-18),

When I modify the example code as
ifort:
REAL*8 pi, pi2
pi = ATAN(1.d0)*4.d0
pi2 = 3.141592653589793d0
OPEN(10,FILE='rfile',FORM='UNFORMATTED')
WRITE(10) pi, pi2
CLOSE(10)
g77:
REAL*8 pi, pi2
OPEN(10,FILE='rfile',FORM='UNFORMATTED')
READ(10) pi, pi2
CLOSE(10)
WRITE(*,*) pi, pi2

then I have
8.61919951E+97 3.44827273E-313

Does ifort add the extra bit into header of binary file ?
0 Kudos
Steven_L_Intel1
Employee
1,291 Views
64-bit g77 uses a 64-bit record length all the time. This is not the way most other 64-bit Fortran compilers on Linux/Unix work. There is no option in ifort to write record lengths compatible with 64-bit g77.

ifort has options of writing binary files without any record lengths, but I don't know if g77 can read them.

You are using a five-year-old version of gcc. A more recent one would include gfortran that can read and write unformatted files compatible with ifort.
0 Kudos
eigo
Beginner
1,291 Views
Hi,

Your suggestion makes sense. I confirmed gfortran can read correctly.
(but using old version of gfortran, option -frecord-marker=4 will be needed.)

BTW I found that using stream access in ifort, e.g.
REAL*8 pi, pi2
OPEN(10,FILE='rfile',FORM='unformatted',access='stream')
WRITE(10,POS=9) pi, pi2
CLOSE(10)
g77 can read it correctly. But I do not know it is general or just this case.

Eigo
0 Kudos
Steven_L_Intel1
Employee
1,291 Views
Hmm - what you're doing is skipping over what g77 wants for the record length but doesn't it complain when it doesn't find a valid length?

If you don't use POS= you can write just the data. Perhaps g77 has a way of dealing with that, I don't know. Why are you insisting on using g77 when you have gfortran?
0 Kudos
Reply