Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 18 Dec 2009 08:27:45 +0000
From:      Matthew Seaman <m.seaman@infracaninophile.co.uk>
To:        Anton Shterenlikht <mexas@bristol.ac.uk>
Cc:        freebsd-questions@freebsd.org
Subject:   Re: editing a binary file
Message-ID:  <4B2B3D01.4050304@infracaninophile.co.uk>
In-Reply-To: <20091218012918.GA71118@mech-cluster241.men.bris.ac.uk>
References:  <20091218005102.GA51064@mech-cluster241.men.bris.ac.uk>	<4B2AD666.9090404@lazlarlyricon.com> <20091218012918.GA71118@mech-cluster241.men.bris.ac.uk>

next in thread | previous in thread | raw e-mail | index | archive | help
This is an OpenPGP/MIME signed message (RFC 2440 and 3156)
--------------enigBD213151F79D075CCBE7A555
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: quoted-printable

Anton Shterenlikht wrote:
> On Fri, Dec 18, 2009 at 02:09:58AM +0100, Rolf Nielsen wrote:
>> Anton Shterenlikht wrote:
>>> I'm creating binary files in fortran.
>>> Fortran adds 4 byte record delimiters at the beginning
>>> and the end of each record, which, in the case of a binary
>>> file, is just at the beginning and at the end of the file.
>>> I need to delete these record delimiters, because the
>>> software I use to visualise the binary files interprets
>>> them as data. But I don't know how. I've looked at
>>> hexdump and od, but those are only dumping (I think)
>>> file contents, and I cannot see how to edit a file with them.
>>>
>>> Any advice?
>>>
>>> many thanks
>>> anton
>>>
>> Hello Anton,
>>
>> My bet would be /usr/ports/editors/hexedit. Been a while since I've us=
ed=20
>> it, but AFAIR, it has a curses or a curses like interface, and it's=20
>> fairly simple to use, yet sufficiently powerful for most normal binary=
=20
>> editing. If you want a GUI, I believe gnome (and probably KDE as well)=
=20
>> has its own hex editor.
>=20
> thank you. hexedit does the job on small files, but is quite
> clunky. If I've a xGB file and I need to delete the first and
> the last record, this becomes quite hard, if at all possible.
>=20
> I didn't appreciate it's not that simple.
>=20
> Perhaps I can read a file with C and write back? I can't
> remember if C supports binary files, and whether it
> also writes some record delimiters.

Sure, you can write a fairly short C program to do this.  In fact,
it's pretty easy in perl too:

#!/usr/bin/perl -w

use Fcntl;
use constant BUFFSIZ =3D> 4096;

for my $file (@ARGV) {
    my $buffer     =3D '';
    my $bytes_read =3D 0;

    sysopen INFILE, $file, O_RDONLY
        or die "Failed to open file $file for reading -- $!\n";
    sysseek INFILE, 4, 0;  # skip first 4 bytes
    sysopen OUTFILE, "${file}.out", O_WRONLY|O_CREAT
        or die "Failed to open file ${file}.out for writing -- $!\n";
    while ( $bytesread =3D sysread INFILE, $buffer, BUFFSIZ, 0 ) {
        # If we don't read 4096 bytes, try a second read: if this
        # returns zero, then we're at EOF
	if ( $bytes_read < BUFFSIZ ) {
	    my $offset =3D $bytes_read;
            $bytes_read =3D sysread INFILE, $buffer, BUFFSIZ, $offset;

            if ( $bytes_read =3D=3D 0 ) {
                # Trim the last 4 bytes
                substr ($buffer, -4) =3D ''; # Trim off last 4 bytes
            }
	}
        syswrite OUTFILE, $buffer;
    }
    close INFILE;
    close OUTFILE;
}


Untested, and needs more error checking around those sysread()s and syswr=
ite()s,
but it should give you the general idea.

	Cheers,

	Matthew

--=20
Dr Matthew J Seaman MA, D.Phil.                   7 Priory Courtyard
                                                  Flat 3
PGP: http://www.infracaninophile.co.uk/pgpkey     Ramsgate
                                                  Kent, CT11 9PW


--------------enigBD213151F79D075CCBE7A555
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: OpenPGP digital signature
Content-Disposition: attachment; filename="signature.asc"

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.13 (FreeBSD)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEAREIAAYFAksrPQcACgkQ8Mjk52CukIwGkgCfZKYcJViO1YcUErL36GE8uEYP
vA0An0EpJ1qG0DrATmyX4os9Wbngsc8h
=bBz/
-----END PGP SIGNATURE-----

--------------enigBD213151F79D075CCBE7A555--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?4B2B3D01.4050304>