need help reading fortran unformatted file from a c program 
Author Message
 need help reading fortran unformatted file from a c program

Hello:

I'm trying to read an unformatted file created by a fortran
program into a c program that does not have access to the fortran
libraries. the data values in the file are supposed to be real
and ascii, and i've been given the order they should appear.  Since the
data representations for fortran real*4's and c floats is supposed to
be the same, I had assumed I could read the real
values with some code like:

    if ((fp = fopen(fileName, "r")) == (FILE *) NULL)
    {
      fprintf(stderr, "Could not open file %s\n", fileName);
      exit(-1);
    }

    fread((char *) &length,4 , 1,fp);
    fread((char *) &revision,4 , 1,fp);

where length and revision are declared as floats, but i don't get sensible
results. The following fortran code does read the files:

      OPEN (UNIT=ILFN, FILE=IFILE, STATUS='OLD', FORM='UNFORMATTED',
     * IOSTAT=STATUS)
      READ(ILFN, IOSTAT=STATUS, ERR=8000) IBUF(1),(IBUF(I),I=2,LREC)

where IBUF is real*4 array, and LREC is equivalence'd to IBUF(1).

Any help much appreciated.

--
Greg Garland - Alive, occupying space, and exerting gravitational force

MS 62-024, Harris Semiconductor Sector, PO Bx 883,

"Never let the facts interfere with your perception of reality."



Sat, 08 Jun 1996 02:26:38 GMT  
 need help reading fortran unformatted file from a c program
Thanks for the replies to my question. Turns out that i was being
bit by a big/little endian conflict between a Sun and Dec box.  A
little fudging around with bit operations and i got the bytes for
the float's and int's into the neccessary order.

--
Greg Garland - Alive, occupying space, and exerting gravitational force

MS 62-024, Harris Semiconductor Sector, PO Bx 883,

"Never let the facts interfere with your perception of reality."



Mon, 10 Jun 1996 12:31:43 GMT  
 need help reading fortran unformatted file from a c program

Quote:
>Hello:
>I'm trying to read an unformatted file created by a fortran
>program into a c program that does not have access to the fortran
>libraries. the data values in the file are supposed to be real
>and ascii, and i've been given the order they should appear.  Since the
>data representations for fortran real*4's and c floats is supposed to
>be the same, I had assumed I could read the real
>values with some code like:
>    if ((fp = fopen(fileName, "r")) == (FILE *) NULL)

In microsoft C at least, the file MODE, when not specified, is taken from
the _fmode variable, which is in turn defaulted to O_TEXT in one of the
libraries (stdlib.h) . This may be the cause of your problem. If not, it may
be that the formats simply aren't the same.
ie. Try fopen( filename, "rb" ) etc etc. Not sure if it changes the behaviour
of an fread like that! Anyone?...


Sat, 08 Jun 1996 19:36:58 GMT  
 need help reading fortran unformatted file from a c program
I haven't read the original post, but one potential problem in
reading an unformatted fortran file is that fortran adds  
4-byte integers specifying the record length at the beginning and the
end of each record specifying the record length. You will have to take
this into account when you write your C reads.

        *********************************************************
       *                                                       *
      * ///////    /////    //   //   //           //////     *
     * //    //  //   //   //   //   //           //         *
    * ///////   ///////   //   //   //            //////    *
   * //        //   //   //   //   //                 //   *
  * //        //   //    /////    ////////      /////// # *
 *                                                       *
*********************************************************



Tue, 11 Jun 1996 17:30:51 GMT  
 need help reading fortran unformatted file from a c program
You should find out exactly what the format written by the Fortran program was
and what your C compiler expects.  Critical issues are the exponent, the bias
factor on the exponent, the the sign of the exponent and the sign of the mantissa.
If you are unlucky, you will have to use bit fields to pick apart the Fortran
representation and then reconstruct in in a C float variable.

I agree, also, that you should open the file for reading in binary mode so that
non of the bits get converted.  If you have to convert the file, then read the
variables in as char's.



Sat, 15 Jun 1996 21:59:51 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Reading Fortran unformatted files in C.

2. Reading Unformatted Fortran using C

3. Need help with a program that reads ZIP files

4. reading fortran data file from C program

5. HELP!! fread reads Fortran Direct files incorrectly

6. Please help: reading FORTRAN files using C code

7. Help with Emulating a FORTRAN File Read Function in C

8. Need help: call fortran routine in C programs

9. Need help on using tapedrive from C/Fortran program

10. How to read Fortran "SEQUENTIAL UNFORMATTED" files

11. READING UNFORMATTED INPUT

12. Reading unformatted SCSI in Windows

 

 
Powered by phpBB® Forum Software