Read out the content of a Fortran binary file based on the limited information given by its hexadecimal representation

Here is a C program to read unformatted files. The 40-byte records appear to be character strings, the 16 byte records contain pairs of double precision reals, and the other records contain one or two 4-byte integers. Note that I said “appear to”. When I see the 8 bytes 00 00 00 00 00 00 F0 3F, I recognize that as double precision 1.0. Similarly for other integers and reals.

/* Read Fortran unformatted file with record markers containing byte sizes */
#include <stdio.h>
int main(int argc, char *argv[]){
int recn,recl,nin; char buf[0x1000]; long offset;
FILE *fil=fopen(argv[1],"rb");
recn=0; offset = 0;
do{
   nin = fread(&recl,4,1,fil); // prefix marker
   if(nin < 1)break;
   offset+=4;
   if(nin > 0x1000){
      fprintf(stderr,"Buffer is too small, need more than 4096 bytes\n");
      exit(1);
      }
   nin=fread(buf,1,recl,fil);
   fread(&recl,4,1,fil); // postfix marker
   recn++; printf("%4d %8d %12ld\n",recn,nin,offset);
   offset+=nin+4;
   }while(1);
fclose(fil);
}

1 Like