comp.lang.ada
 help / color / mirror / Atom feed
From: jerry@jvdsys.nextjk.stuyts.nl (Jerry van Dijk)
Subject: Re: File IO
Date: 1997/12/03
Date: 1997-12-03T00:00:00+00:00	[thread overview]
Message-ID: <881122990.89snx@jvdsys.nextjk.stuyts.nl> (raw)
In-Reply-To: 348345B4.6E26@eng.clemson.edu


In article <348345B4.6E26@eng.clemson.edu> xianchl@eng.clemson.edu writes:

>Does anyone who knows how to use file IO in ada 95? I am learning
>Ada95, and I have tried using Sequential_IO and Direct_IO, but when
>I use read(Input_File, Value), I cannot get the same one as in the
>data file.  If you have some experience on this, would you mind giving
>me some suggestions?  It will be better if you can attach a piece of
>code which works.

It might have been easier if you posted an example and what you expected
it to do, so we might have pointed out the error.

However, since you asked...

   -- File I/O demo

   with Ada.Text_IO;
   with Ada.Sequential_IO;

   procedure File_Demo is

      Data_Size : constant Integer := 5;
      Test_File : constant String  := "test.dat";

      type Data_Array is array (1 .. Data_Size) of Integer;

      package Integer_File is new Ada.Sequential_IO (Data_Array);

      Data_To_Write, Data_Read : Data_Array := (others => 0);

      procedure Write_Data (File_Name : in String;
                            Data      : in Data_Array) is
         My_File : Integer_File.File_Type;
      begin
         Integer_File.Create (File => My_File,
                              Mode => Integer_File.Out_File,
                              Name => File_Name);
         Integer_File.Write  (File => My_File,
                              Item => Data);
         Integer_File.Close  (File => My_File);
      end Write_Data;

      procedure Read_Data (File_Name : in     String;
                           Data      :    out Data_Array) is
         My_File : Integer_File.File_Type;
      begin
         Integer_File.Open  (File => My_File,
                             Mode => Integer_File.In_File,
                             Name => File_Name);
         Integer_File.Read  (File => My_File,
                             Item => Data);
         Integer_File.Close (My_File);
      end Read_Data;

   begin

      Data_To_Write := (1, 2, 3, 5, 8);

      Write_Data (File_Name => Test_File,
                  Data      => Data_To_Write);

      Read_Data  (File_Name => Test_File,
                  Data      => Data_Read);

      if Data_To_Write = Data_Read then
         Ada.Text_IO.Put_Line ("Test succeded!");
      else
         Ada.Text_IO.Put_Line ("Test failed :-(");
      end if;

   end File_Demo;

--

-- Jerry van Dijk | Leiden, Holland
-- Consultant     | Team Ada
-- Ordina Finance | jdijk@acm.org




      parent reply	other threads:[~1997-12-03  0:00 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <348345B4.6E26@eng.clemson.edu>
1997-12-02  0:00 ` File IO Haug Buerger
1997-12-03  0:00 ` Jerry van Dijk [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