Reading and writing binary files

Bob Margulies
Wed Sep 27 00:35:00 GMT 2006

I'm a little rusty, having been away from Fortran for quite a few years. 
I'd like to do a project in Fortran that involves making a binary 
editor.  This means reading and writing a binary (unformatted) file.  
It's not going smoothly.

Here are some key elements of the gfortran program:

      parameter (nd = 28)
      integer * 1 dat(nd)
      open(11, file = "", iostat = ios,
    1     form = "unformatted", status ="old" ) 
      open(22, file = "abc.yyy", form = "unformatted")
      read(11) dat
      write(22) dat

Here's the contents of the input file  
abcdefghijklmnopqrstuvwxyz123456  It's 32 bytes long.  But Fortran won't 
let me read the whole thing.  That's the reason for nd = 28.  nd = 29 
gives a read error.

When I write out onto abc.yyy the 28 bytes that were just read in, the 
result is 36 bytes long.  It consists of bytes 5 through 32 of, 
preceded and followed by the 4 bytes (hex) 1c 00 00 00 for a total of 36 
bytes.  Note that 1c hex is 28, which is the number of bytes actually read.

My question is, how do I get the output file to be the same as the input?

Operating system is XP home, SP2.

Bob Margulies

More information about the Gcc-help mailing list