comp.lang.ada
 help / color / mirror / Atom feed
From: Thierry Lelegard <thierry.lelegard.NOSPAM@cptechno.NOSPAM.com>
Subject: Re: Read Binary File
Date: Wed, 05 Nov 2003 11:11:32 +0100
Date: 2003-11-05T11:11:32+01:00	[thread overview]
Message-ID: <3FA8CCD4.DEA23A81@cptechno.NOSPAM.com> (raw)
In-Reply-To: 6449213e.0311030756.64346802@posting.google.com

> Situation: OpenVMS, Alpha, Ada '83.  Binary file created by C++ program.
>            Use_Error raised on first byte.  We regularly read Ada-generated
>            binary files, but the C++ source in new.

Classical problem of logical file contents vs. physical file contents.
This is the kind of problem that primitive operating systems (*) with
flat files do not have...

On VMS, the physical content is managed by the file system (XQP) while
the logical content is managed by the Recode Management System (RMS).

Have a look (dir/full) at the binary files which were created by Ada
Sequential_IO (Byte) and by C++. Their attributes are quite different,
this is why they are not interpreted the same way.

I would guess that the Ada-created files have attribute
"fixed record size, 2 bytes". Dump them in this case, I guess
that the content is 1st data byte, zero, 2nd data byte, zero, etc.

The reason is:
- Sequential_IO creates "fixed record size" files.
- RMS always starts a new record on even bytes, using one zero
  byte padding if the record size is odd.

Note that this zero padding is just a "hidden implementation detail"
of the physical content. Reading the file using RMS (which is used
by Sequential_IO) only returns the logical content, that is to say
one byte for each record in your case, since a "record" (logical
unit of the logical content) is one byte.

Note that DEC C and DEC C++ do not use RMS since those "primitive" 
languages were based on the "primitive" operating system they come
from (UNIX). So, unless you use special file creation options, the
C/C++ runtime does not use RMS, it directly use XQP (actually RMS
Block-IO, but this is the same level of content as XQP). C-created
files do not have any "logical" content, they just have a "physical"
content on a VMS standpoint.

To raad them from another language, you have two options (assuming
that you cannot modify the C++ program to generate the files in a
different format):

- Modify the file attributes so that the (numodified) physical content
  matches the implementation of some logical content. Then, read the
  file according to the new logical content. This is a hack but it
  works if carefully understood.

- Do it the simple way and directly read the physical contents.

To implement this in Ada:

First option:
- Force a different logical view of the file using the DCL command:
       set file /attribute=(rfm=fix,mrs=2,lrl=2) filename
  Your file keeps its physical content but the logical interpretation
  of this  physical content has changed to: fixed record size, 2 bytes.
- Instanciate Sequential_IO on a type "array (1..2) of Byte"
  (instead of "Byte").
- Read the content of the file 2 bytes per 2 bytes.
- Warning: If the file size is odd, you will most certainly get
  a Use_Error on the last byte.

Second option (I personally would use this one):
- Give up high-level Ada I/O packages and directly read the physical
  content of the file using RMS Block-IO. This, however, needs some
  familiarity with RMS.

-Thierry
(*) Don't flame, just joking...



      parent reply	other threads:[~2003-11-05 10:11 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-11-03 15:56 Read Binary File Frank Mattison
2003-11-03 16:00 ` Lutz Donnerhacke
2003-11-03 16:37 ` Read Binary File (on Compaq Ada) Larry Kilgallen
2003-11-03 22:34 ` Read Binary File Gautier Write-only
2003-11-04  3:24 ` Steve
2003-11-05 10:11 ` Thierry Lelegard [this message]
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox