comp.lang.ada
 help / color / mirror / Atom feed
* Simple Stream_IO question
@ 1996-10-21  0:00 John Raquet
  1996-10-21  0:00 ` Robert Dewar
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: John Raquet @ 1996-10-21  0:00 UTC (permalink / raw)




I'm trying to use Streams.Stream_IO to read/write multiple record types 
from a single file.  I know it's simple to do, but I can't 
figure out how to do it.  

If someone could show me what I'm doing wrong in the following code 
sample I'd appreciate it:

-----------------
with Ada.Streams.Stream_IO; use Ada.Streams.Stream_IO;

procedure Foo is

   F : Float;
   I : Integer;

   File : File_Type;
   
begin

   Open(File, In_File, "foo.adb");

   Float'Read(File, F);
   Integer'Read(File, I);

   Close(File);

end Foo;


---------------------------------------------------------
John F. Raquet                   jfraquet@acs.ucalgary.ca
Dept of Geomatics Engineering       Phone: (403) 220-7378
University of Calgary                 Fax: (403) 284-1980
---------------------------------------------------------






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

* Re: Simple Stream_IO question
  1996-10-21  0:00 Simple Stream_IO question John Raquet
@ 1996-10-21  0:00 ` Robert Dewar
  1996-10-22  0:00 ` John English
  1996-10-22  0:00 ` Norman H. Cohen
  2 siblings, 0 replies; 4+ messages in thread
From: Robert Dewar @ 1996-10-21  0:00 UTC (permalink / raw)



John provides the following incorrect program:

with Ada.Streams.Stream_IO; use Ada.Streams.Stream_IO;

procedure Foo is

   F : Float;
   I : Integer;

   File : File_Type;

begin

   Open(File, In_File, "foo.adb");

   Float'Read(File, F);
   Integer'Read(File, I);

   Close(File);

end Foo;

You have to pay attention to types in Ada, the Read attribute has the 
profile

6   S'Read
                S'Read denotes a procedure with the following specification:

7                   procedure S'Read(
                       Stream : access Ada.Streams.Root_Stream_Type'Class;
                       Item : out T)


But your first argument is of type Streams.Stream_IO.File_Type which is
completely wrong. 

You ned to use Streams.Stream_IO.Stream!






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

* Re: Simple Stream_IO question
  1996-10-21  0:00 Simple Stream_IO question John Raquet
  1996-10-21  0:00 ` Robert Dewar
@ 1996-10-22  0:00 ` John English
  1996-10-22  0:00 ` Norman H. Cohen
  2 siblings, 0 replies; 4+ messages in thread
From: John English @ 1996-10-22  0:00 UTC (permalink / raw)



John Raquet (jfraquet@acs.ucalgary.ca) wrote:
: If someone could show me what I'm doing wrong in the following code 
: sample I'd appreciate it:

: -----------------
: with Ada.Streams.Stream_IO; use Ada.Streams.Stream_IO;
: procedure Foo is
:    F : Float;
:    I : Integer;
:    File : File_Type;

-- You also need:
     Strm : Stream_Access;

: begin
:    Open(File, In_File, "foo.adb");

-- And now:
     Strm := Stream(File);  -- create a stream based on the file

:    Float'Read(File, F);
:    Integer'Read(File, I);

-- These two lines should read:
     Float'Read(Strm, F);
     Integer'Read(Strm, I);

:    Close(File);
: end Foo;

---------------------------------------------------------------
 John English              | mailto:je@brighton.ac.uk
 Senior Lecturer           | http://www.comp.it.bton.ac.uk/je
 Dept. of Computing        | fax: (+44) 1273 642405
 University of Brighton    |
---------------------------------------------------------------




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

* Re: Simple Stream_IO question
  1996-10-21  0:00 Simple Stream_IO question John Raquet
  1996-10-21  0:00 ` Robert Dewar
  1996-10-22  0:00 ` John English
@ 1996-10-22  0:00 ` Norman H. Cohen
  2 siblings, 0 replies; 4+ messages in thread
From: Norman H. Cohen @ 1996-10-22  0:00 UTC (permalink / raw)



John Raquet wrote:

> If someone could show me what I'm doing wrong in the following code
> sample I'd appreciate it:
> 
> -----------------
> with Ada.Streams.Stream_IO; use Ada.Streams.Stream_IO;
> 
> procedure Foo is
> 
>    F : Float;
>    I : Integer;
> 
>    File : File_Type;
> 
> begin
> 
>    Open(File, In_File, "foo.adb");
> 
>    Float'Read(File, F);
>    Integer'Read(File, I);
> 
>    Close(File);
> 
> end Foo;

The first parameter in a call on a 'Read attribute must be an access
value of a type pointing to Root_Stream'Class.  Package
Ada.Streams.Stream_IO provides such a type, named Stream_Access.  The
package also provides its own type File_Type (the type of File in the
example above) and a function Stream that takes a File_Type parameter
and returns a Stream_Access parameter pointing to a stream that
corresponds to the given file.  Thus you should write:

   Float'Read ( Stream(File), F );
   Integer'Read ( Stream(File), I );

-- 
Norman H. Cohen
mailto:ncohen@watson.ibm.com
http://www.research.ibm.com/people/n/ncohen




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

end of thread, other threads:[~1996-10-22  0:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-10-21  0:00 Simple Stream_IO question John Raquet
1996-10-21  0:00 ` Robert Dewar
1996-10-22  0:00 ` John English
1996-10-22  0:00 ` Norman H. Cohen

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