[comp.sys.sgi] Reading fortran "unformatted" files from C on the 4D.

XBR2D96D@DDATHD21.BITNET (Knobi der Rechnerschrat) (10/25/89)

Hallo,

  here comes a code fragment that reads (from C) a fortran "unformatted"
file. Suppose the file has been fopen'ed to fp and that i1,i2,i3 are int's
and that farr is a float-array. The programm then reads max fortran-records
from the file, each record written by a fortran code like

  write(11) i1,i2,i3,farr    ! with REAL farr(3)

 You have of course to know what is written in the records to interpret them
correctly. For my opinion having the record-size included in the record is not
as bad as somebody on the list told us. If you really need "C"-binary files,
write a "C"-routine.

Regards

Martin Knoblauch
TH-Darmstadt
Phys. Chem. 1
Petersenstrasse 20
D-6100 Darmstadt, F.R.G.
------------------------------------------------------------------------------
for(iz=0;iz<max;iz++){
  nitem = fread(&dummy,4,1,fp);
  fread(&i1,sizeof(int),1,fp);
  fread(&i2,sizeof(int),1,fp);
  fread(&i3,sizeof(int),1,fp);
  fread(&farr[0],sizeof(float),3,fp);
  nitem = fread(&dummy,4,1,fp);
  }
------------------------------------------------------------------------------

jra@BRL.MIL ("John R. Anderson", VLD/ASB) (10/26/89)

Martin Knoblauch writes:

> for(iz=0;iz<max;iz++){
>   nitem = fread(&dummy,4,1,fp);
>   fread(&i1,sizeof(int),1,fp);
>   fread(&i2,sizeof(int),1,fp);
>   fread(&i3,sizeof(int),1,fp);
>   fread(&farr[0],sizeof(float),3,fp);
>   nitem = fread(&dummy,4,1,fp);
>   }

Note that although this will work on an IRIS 4D, not all fortran
compilers create "unforamtted" files with  the record length repeated
at the beginning and end of the record. I have seen some that only
include the record length at the start of each record. So depending
on this will create non-portable code.


				-John

mccalpin@masig3.masig3.ocean.fsu.edu (John D. McCalpin) (10/26/89)

Martin Knoblauch writes:
> for(iz=0;iz<max;iz++){
>   nitem = fread(&dummy,4,1,fp);
>   fread(&i1,sizeof(int),1,fp);
>   fread(&i2,sizeof(int),1,fp);
>   fread(&i3,sizeof(int),1,fp);
>   fread(&farr[0],sizeof(float),3,fp);
>   nitem = fread(&dummy,4,1,fp);
>   }

In article <8910251818.aa09277@VAT.BRL.MIL>  jra@BRL.MIL ("John R.
Anderson", VLD/ASB) replies:
>Note that although this will work on an IRIS 4D, not all fortran
>compilers create "unforamtted" files with  the record length repeated
>at the beginning and end of the record. I have seen some that only
>include the record length at the start of each record. So depending
>on this will create non-portable code.
>				-John

That's why you have to use the same C routines to write the data,
too....  Having IEEE binary compatibility does not do you any good if
the record structures differ, and only C allows you to be absolutely
specific about what you are writing and reading....

By the way, on the 4D machines, the control words can be eliminated by
writing the file as "access='direct'" rather than "access='sequential'".
Of course all the records have to be the same length in this case....
Direct access files have _no_ record marks of any type -- they are
just like the "form='binary'" files on the IRIS 3000, and in fact are
transportable between FORTRAN applications on the two families of
machines....
--
John D. McCalpin - mccalpin@masig1.ocean.fsu.edu
		   mccalpin@scri1.scri.fsu.edu
		   mccalpin@delocn.udel.edu