comp.lang.ada
 help / color / mirror / Atom feed
From: Laurent <daemon2@internet.lu>
Subject: Re: Reading data from file
Date: Tue, 4 Mar 2014 13:27:25 -0800 (PST)
Date: 2014-03-04T13:27:25-08:00	[thread overview]
Message-ID: <9b780534-04fb-43c8-b8de-1610421c471d@googlegroups.com> (raw)
In-Reply-To: <orIsCUDPahFTFwJN@ada-augusta.demon.co.uk>

Hi

@Mike: Thanks for this detailed example. 

Unfortunately I seem to have mixed up a few things while writing my post. 

The text file contains just:
   
  1234
  test
  male ...

The ID: ,... was just supposed to inform you about the type. 

The exact error message is: 

raised ADA.IO_EXCEPTIONS.DATA_ERROR : a-tienio.adb:57 instantiated at employees-io.adb:17

which is:

    type GenderType is (Female, Male);     

     package GenderType_IO is
     new Ada.Text_IO.Enumeration_IO (Enum => GenderType);

Here is my code. It compiles but it isn't complete. Will probably fail for the moment if I try to read more than one record. (Won't read even one so...). Reading the name could be one reason for the problem with the enumeration. 

procedure Read_Employee_From_File (Item : out Employees.Employee) is

      Import_File : Ada.Text_IO.File_Type;
      Temp_Record : Employees.Employee;
      Name_Length: Natural;

   begin -- Read_Employee_From_File

      -- open the file and associate it with the file variable name
      Ada.Text_IO.Open (File => Import_File, Mode => Ada.Text_IO.In_File,
                                   Name => "Datafile.txt");

      -- read each data item

      loop
         exit when Ada.Text_IO.End_Of_File (Import_File);

         Ada.Integer_Text_IO.Get (File => Import_File, Item => Temp_Record.ID);
         Ada.Text_IO.Get_Line (File => Import_File, Item => Temp_Record.Name, Last => Name_Length);
         GenderType_IO.Get (File => Import_File, Item => Temp_Record.Gender);
         Ada.Integer_Text_IO.Get (File => Import_File, Item => Temp_Record.NumDepend);
         Currency.IO.Get (File => Import_File, Item => Temp_Record.Salary);
         Dates.IO.Get (File => Import_File, Item => Temp_Record.StartDate);

         Item := Temp_Record;

      end loop;


   end Read_Employee_From_File;

There is an other exercise in the book which uses this construction too and which works. Just reads a string and an int  until the end of the file. 

@Adam: I think I have to put a line to display what my construction is reading/and the content of Temp_Record just before the program terminates.


Other question: I have a working procedure to get the same information but from terminal which also checks if the data is correctly formatted. Is it possible to reroute the info from the file to this procedure? If yes how would I do that.

Because I (rather the author of the book) have this procedures which seem to do the same thing (or the other way around. Don't really understand why there are 2 procedures for the same thing).  Just that until now I didn't read anything from a file.


procedure Get (File : in Ada.Text_IO.File_Type; Item : out Quantity) is
      F : Float;
   begin -- get

      -- just read it as a float quantity, then convert
      Ada.Float_Text_IO.Get (File => File, Item => F);
      Item := MakeCurrency (F);

   end Get;

 procedure Get (Item : out Quantity) is
   begin -- get
      Get (File => Ada.Text_IO.Standard_Input, Item => Item);
   end Get;

Thanks

Laurent

PS: Will never again post a message from my smartphone. crap onscreen keyboard/tiny screen. Not made for browsing a forum.

  reply	other threads:[~2014-03-04 21:27 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-03-04  9:46 Reading data from file Laurent
2014-03-04 16:17 ` adambeneschan
2014-03-04 18:08 ` Mike H
2014-03-04 21:27   ` Laurent [this message]
2014-03-04 21:35     ` Laurent
2014-03-04 21:42     ` Niklas Holsti
2014-03-05 13:59     ` Mike H
2014-03-05 20:33       ` Laurent
2014-03-05 21:00         ` Jeffrey Carter
2014-03-05 21:13           ` Laurent
2014-03-05 21:25             ` Niklas Holsti
2014-03-05 21:56               ` Laurent
2014-03-06  8:35                 ` Dmitry A. Kazakov
2014-03-07 21:55                   ` Laurent
2014-03-08  6:56                     ` Dmitry A. Kazakov
2014-03-08 21:21                       ` Laurent
2014-03-09 22:39                         ` Laurent
2014-03-10  2:42                           ` Jeffrey Carter
2014-03-11 20:54                             ` Laurent
2014-03-08  9:00                     ` Simon Wright
replies disabled

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