comp.lang.ada
 help / color / mirror / Atom feed
* Read Binary File
@ 2003-11-03 15:56 Frank Mattison
  2003-11-03 16:00 ` Lutz Donnerhacke
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Frank Mattison @ 2003-11-03 15:56 UTC (permalink / raw)


Does anyone have a suggestion for reading this binary file?  

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.    
  
with Sequential_IO;
with Text_IO;
procedure Test_Sio is
   type Byte is range 0..255;
   for Byte'Size use 8;
   package Byte_IO is new Sequential_IO(Byte);
   Test_Byte : Byte;
   File : Byte_IO.File_Type;
begin
   Byte_Io.Open (File => File, Mode => Byte_Io.In_File, Name => "TEST.BIN");
   Trap:
   begin
      Byte_IO.Read(File => File, Item => Test_Byte);
      exception
         when Byte_IO.Use_Error =>
            Text_IO.Put_Line("Use Error raised on Byte_IO.Read");
            Byte_Io.Close (File => File);
   end Trap;
   exception
   when others =>
      Text_IO.Put_Line("Other exception raised");
      Byte_Io.Close (File => File);
end Test_Sio;

Result:
>run test_sio
Use Error raised on Byte_IO.Read

>dump/byte/page test.bin
 00 00 07 D3 34 35 35 37 00 00 05 C8 00 D2 00 01 ..�.�...7554�... 000000
 3E 08 BD B3 53 AE C1 56 00 00 CA D8 00 00 00 F7 �...��..V��S��.> 000010
 A4 13 65 CF FD C1 41 7A 12 7E C8 E7 A4 6D C1 36 6�m���~.zA���e.� 000020
                                                      
Have tried essentially all variations on the file attributes and formats  
beginning with these, which work for files created by Ada:  

Record format:      Variable length, maximum 0 bytes, longest 1480 bytes
Record attributes:  Carriage return carriage control
RMS attributes:     None

Record format:      Stream_LF, maximum 0 bytes, longest 32767 bytes
Record attributes:  Carriage return carriage control
RMS attributes:     None

Thanks for any suggestion!

Frank Mattison



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Read Binary File
  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
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Lutz Donnerhacke @ 2003-11-03 16:00 UTC (permalink / raw)


* Frank Mattison wrote:
>    type Byte is range 0..255;
>    for Byte'Size use 8;

I don't know Ada83, but this seems strange.



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Read Binary File (on Compaq Ada)
  2003-11-03 15:56 Read Binary File Frank Mattison
  2003-11-03 16:00 ` Lutz Donnerhacke
@ 2003-11-03 16:37 ` Larry Kilgallen
  2003-11-03 22:34 ` Read Binary File Gautier Write-only
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Larry Kilgallen @ 2003-11-03 16:37 UTC (permalink / raw)


In article <6449213e.0311030756.64346802@posting.google.com>, fmattison@ssd5.nrl.navy.mil (Frank Mattison) writes:
> Does anyone have a suggestion for reading this binary file?  
> 
> 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.    
>   
> with Sequential_IO;
> with Text_IO;

How about Sequential_Mixed_IO ?



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Read Binary File
  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 ` Gautier Write-only
  2003-11-04  3:24 ` Steve
  2003-11-05 10:11 ` Thierry Lelegard
  4 siblings, 0 replies; 6+ messages in thread
From: Gautier Write-only @ 2003-11-03 22:34 UTC (permalink / raw)


Frank Mattison:

# Does anyone have a suggestion for reading this binary file?
# 
# 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.

If I remember well my painful experiences with the
tandem {DEC Ada & Sequential_IO}, there is one carriage return
expected after each element (in your case, each byte!).
Try to write some date with your Byte_IO and see the result
in a binary editor...
I suspect Larry's reply is the good way (hum, but specific to DEC Ada...)
________________________________________________________
Gautier  --  http://www.mysunrise.ch/users/gdm/gsoft.htm

NB: For a direct answer, e-mail address on the Web site!



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Read Binary File
  2003-11-03 15:56 Read Binary File Frank Mattison
                   ` (2 preceding siblings ...)
  2003-11-03 22:34 ` Read Binary File Gautier Write-only
@ 2003-11-04  3:24 ` Steve
  2003-11-05 10:11 ` Thierry Lelegard
  4 siblings, 0 replies; 6+ messages in thread
From: Steve @ 2003-11-04  3:24 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 2701 bytes --]

Do the Ada-generated files also have the Record format: Stream_LF?

I faced a similar problem on VAX VMS a number of years ago.  I can't
remember exactly how I solved it, but I believe I had to make special system
calls to handle Stream_LF files.  C and C++ like to view files as streams of
bytes.

VMS has more of a record oriented file system.  Mapping between the two
takes a little extra work.

From the Ada LRM:  "The exception Use_Error is propagated if an operation is
attempted that is not possible for reasons that depend on the
characteristics of the external file."

I've never used VAX Ada (I used VAX Pascal). Perhaps there is some "Form"
selection you can make to indicate the files are "Stream_LF" files.

Steve
(The Duck)


"Frank Mattison" <fmattison@ssd5.nrl.navy.mil> wrote in message
news:6449213e.0311030756.64346802@posting.google.com...
> Does anyone have a suggestion for reading this binary file?
>
> 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.
>
> with Sequential_IO;
> with Text_IO;
> procedure Test_Sio is
>    type Byte is range 0..255;
>    for Byte'Size use 8;
>    package Byte_IO is new Sequential_IO(Byte);
>    Test_Byte : Byte;
>    File : Byte_IO.File_Type;
> begin
>    Byte_Io.Open (File => File, Mode => Byte_Io.In_File, Name =>
"TEST.BIN");
>    Trap:
>    begin
>       Byte_IO.Read(File => File, Item => Test_Byte);
>       exception
>          when Byte_IO.Use_Error =>
>             Text_IO.Put_Line("Use Error raised on Byte_IO.Read");
>             Byte_Io.Close (File => File);
>    end Trap;
>    exception
>    when others =>
>       Text_IO.Put_Line("Other exception raised");
>       Byte_Io.Close (File => File);
> end Test_Sio;
>
> Result:
> >run test_sio
> Use Error raised on Byte_IO.Read
>
> >dump/byte/page test.bin
>  00 00 07 D3 34 35 35 37 00 00 05 C8 00 D2 00 01 ..�.�...7554�... 000000
>  3E 08 BD B3 53 AE C1 56 00 00 CA D8 00 00 00 F7 �...��..V��S��.> 000010
>  A4 13 65 CF FD C1 41 7A 12 7E C8 E7 A4 6D C1 36 6�m���~.zA���e.� 000020
>
> Have tried essentially all variations on the file attributes and formats
> beginning with these, which work for files created by Ada:
>
> Record format:      Variable length, maximum 0 bytes, longest 1480 bytes
> Record attributes:  Carriage return carriage control
> RMS attributes:     None
>
> Record format:      Stream_LF, maximum 0 bytes, longest 32767 bytes
> Record attributes:  Carriage return carriage control
> RMS attributes:     None
>
> Thanks for any suggestion!
>
> Frank Mattison





^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Read Binary File
  2003-11-03 15:56 Read Binary File Frank Mattison
                   ` (3 preceding siblings ...)
  2003-11-04  3:24 ` Steve
@ 2003-11-05 10:11 ` Thierry Lelegard
  4 siblings, 0 replies; 6+ messages in thread
From: Thierry Lelegard @ 2003-11-05 10:11 UTC (permalink / raw)


> 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...



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2003-11-05 10:11 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox