Table of Contents


unfscan, unfread, unfwrite, unfswap, unfsize - unformatted FORTRAN I/O data access from C


#include <stdinc.h>#include <unfio.h>int unfscan(stream instr)
int unfread(stream instr, char *buf, int bufsize)
int unfwrite(stream instr, char *buf, int bufsize)
int unfswap(bool do_swap)
int unfsize(int size)

Descriptionunfio are a set of routines that allow you to (sequentially)
access  binary files that have been created with most FORTRAN compilers
 in unformatted I/O access. The datafile should have been created with an
form=’unformatted’ OPEN statement. unfscan can be used to scan unformatted
records, returning the size (in bytes) of the data-section of each record.
End of file and various others  errors are returned as 0 and some negative
number resp.  unfread can be used in the same way, except you give it a
buffer in which the data can be stored. In good old fortran tradition you
must know the largest buffer size this routine can expect to see. unfwrite
is the opposite of read. The unfsize routine can be set to change the default
UNFIO_HDR_SIZE header size (as determined during NEMO configure and install).
Most older compilers use 4, whereas gfortran use 8. There is also suppor
for 0, which means pure headerless raw data can be read this way. 
For example,
the file created from the following snippet of code 

        INTEGER unit,n
        REAL x,y(3),z(n)
        WRITE(unit) n
        WRITE(unit) x,y,z
can be read with the following snippet of C code:
        char buf[MAXBUF];
        int *ip = (int *) buf;
        float *fp = (float *) buf;
        double *dp = (double *) buf;
        int n;
        float x, y[3], z[MAXN];
        instr = stropen(...,"r");
        count = unfread(instr,buf,MAXBUF);
        if (count < 1) error("first block");
        n = ip[0];
        count = unfread(instr,buf,MAXBUF);
        if (count < 1) error("second block");
        x = fp[0];
        y[0] = fp[1]; y[1] = fp[2]; y[2] = fp[3];
        for (i=0; i<n; i++)
            z[i] = fp[4+i];


Currently only tested on a limited number of machines (SunOS, HP/UX), or any machine where unformatted binary output surrounds the data with 4 bytes designating the size of the data written, i.e. what a single WRITE statement block writes to disk can be represented as:
    struct block {
        int size;
        char buf[size];
        int size;
    } block

Access is sequential. If the user keeps a record of the file pointer, and knows the low level filestructure is that of block described above, a call to fseek(3) can in principle be used to re-read previous blocks.


Alignment restrictions on some architectures (e.g. sparc) may make reading certain datafiles hard. This feature has not been implemented. It is up to the user to define their data-alignment structures/unions, or perform the necessary data copy before casting is attempted.

See Also



Peter Teuben


~/src/fortran/      unfio.c

Update History

xx-may-94    V1.0 Created for prun           PJT
8-aug-95    V1.1 added support for swap for NBODY5 (u3tos)    PJT
1-mar-06    V1.2 added unfsize for gfortran     PJT
4-mar-06    V1.3 use a default UNFIO_HDR_SIZE for hdr_size    PJT
8-may-08    V1.4 support raw header=0 data    PJT
25-feb-09    V1.5 add non-swapping version of unfwrite    PJT

Table of Contents