editing a binary file

Nick Barnes Nick.Barnes at pobox.com
Fri Dec 18 11:22:17 UTC 2009


If your Fortran file has the same word size and enddian-ness as your
C, this simple program convert.c will strip all the record length
fields.  I just knocked it up now, no warranty, etc, but it works for
me.  Use as a pipe:

$ ls
convert.c       test.f
$ gcc -Wall -Werror -ansi -pedantic convert.c -o convert
$ gfortran44 test.f -o test
$ ./test
$ ls -l test-output
-rw-r--r--  1 nb  nb  2460 Dec 18 11:17 test-output
$ ./convert < test-output > test-converted
$ ls -l test-converted
-rw-r--r--  1 nb  nb  2420 Dec 18 11:18 test-converted
$

The code does a fair amount of checking; if you get one of the error
messages, let us know.  The most obvious unchecked problem is a short
read, which will complain about mismatched lengths.

If your Fortran has different word sizes or enddian-ness (e.g. most of
the Fortran output files I use on the Clear Climate Code project
<http://clearclimatecode.org/> are generated on big-endian machines),
you will need to add code to tweak the 'size' value after reading it,
and when checking the record-end marker.

Nick B

/* convert.c: remove record length fields from Fortran output file. */
/* Nick Barnes, Ravenbrook Limited, 2009-12-18 */

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <assert.h>

int main(void)
{
        long size;
        char *buf;
        ssize_t bytes;
        assert(sizeof(size) == 4);
        while(bytes = read(0, (void*)&size, sizeof(size))) {
                if (bytes < 0) {
                        fprintf(stderr, "read() returned %ld\n", bytes);
                        exit(1);
                }
                if (size <= 0) {
                        fprintf(stderr, "Read bad record length %ld\n", size);
                        exit(1);
                }
                buf = (char*)malloc(size + sizeof(size));
                if (!buf) {
                        fprintf(stderr, "Couldn't allocate buffer of %ld bytes\n",
                                size + sizeof(size));
                        exit(1);
                }
                bytes = read(0, buf, size + sizeof(size));
                if (bytes <= 0) {
                        fprintf(stderr, "read() returned %ld\n", bytes);
                        exit(1);
                }
                if ((*(long*)(buf+size)) != size) {
                        fprintf(stderr, "Mismatched record lengths: %ld, %ld\n",
                                size, *(long*)(buf+size));
                        exit(1);
                }
                write(1, buf, size);
                free(buf);
        }
        return 0;
}





More information about the freebsd-questions mailing list