comp.lang.ada
 help / color / mirror / Atom feed
From: jerry@jvdsys.nextjk.stuyts.nl (Jerry van Dijk)
Subject: Re: Binary Files in Text Editor
Date: 1998/11/14
Date: 1998-11-14T00:00:00+00:00	[thread overview]
Message-ID: <F2F7GK.2v@jvdsys.nextjk.stuyts.nl> (raw)
In-Reply-To: 72f0os$23v$1@nnrp1.dejanews.com

robinsoj@my-dejanews.com wrote:

: I have an array of records (there are two string fields, an integer field, and
: a float field) that I converted from a text file to a binary file.  Now I want
: to display those records to the screen like a list.

The problem in the discussion seems to be the persistent use of the term
'binary file'.

For reading/writing records Ada offers several options (Direct_IO,
Sequential_IO and Stream_IO). The easiest is using sequential IO, see
example below.

So, the question is: is something like the example below what you meant,
and why do you need a 'binary file'.

Jerry.

----- records.ads -----------------------------------------------------

package Records is

   type Data_Type is
      record
         Name   : String (1 .. 8);
         Active : Boolean;
         Salary : Float;
      end record;

   File_Name : constant String := "data.dat";

end Records;

----- write.adb -------------------------------------------------------

with Records;
with Ada.Sequential_IO;

procedure Write is

   package Data_Files is new Ada.Sequential_IO (Records.Data_Type);
   use Data_Files;

   File : File_Type;
   Data : array (1 .. 3) of Records.Data_Type;

begin

   Data(1) := ("Jerry   ", True,  120_000.00);
   Data(2) := ("Carolien", True,   40_000.00);
   Data(3) := ("Peter   ", False,  65_000.00);

   Create (File, Out_File, Records.File_Name);
   for I in Data'Range loop
      Write (File, Data(I));
   end loop;
   Close (File);

end Write;

----- read.adb --------------------------------------------------------

with Records;
with Ada.Text_IO;
with Ada.Sequential_IO;

procedure Read is

   package Data_Files is new Ada.Sequential_IO (Records.Data_Type);
   use Data_Files;

   File : File_Type;
   Data : Records.Data_Type;

begin

   Open (File, In_File, Records.File_Name);

   while not End_Of_File (File) loop

      Read (File, Data);

      Ada.Text_IO.Put (Data.Name & ASCII.HT);
      Ada.Text_IO.Put (Boolean'Image (Data.Active) & ASCII.HT);
      Ada.Text_IO.Put (Float'Image (Data.Salary));
      Ada.Text_IO.New_Line;

   end loop;

   Close (File);

end Read;


-- 
-- Jerry van Dijk  | email: jdijk@acm.org
-- Leiden, Holland | member Team-Ada
-- Ada & Win32: http://stad.dsl.nl/~jvandyk




  parent reply	other threads:[~1998-11-14  0:00 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-11-10  0:00 Binary Files in Text Editor robinsoj
1998-11-11  0:00 ` Dizzy Casablanca
1998-11-11  0:00 ` David C. Hoos, Sr.
1998-11-11  0:00 ` John McCabe
1998-11-11  0:00 ` robinsoj
1998-11-12  0:00   ` John McCabe
1998-11-12  0:00     ` robinsoj
1998-11-13  0:00       ` John McCabe
1998-11-14  0:00       ` Jerry van Dijk [this message]
1998-11-11  0:00 ` Marc A. Criley
replies disabled

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