comp.lang.ada
 help / color / mirror / Atom feed
From: Niklas Holsti <niklas.holsti@tidorum.invalid>
Subject: Re: Reading data from file
Date: Tue, 04 Mar 2014 23:42:37 +0200
Date: 2014-03-04T23:42:37+02:00	[thread overview]
Message-ID: <bnmvmbFc1ikU1@mid.individual.net> (raw)
In-Reply-To: <9b780534-04fb-43c8-b8de-1610421c471d@googlegroups.com>

On 14-03-04 23:27 , Laurent wrote:
> 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.

I believe you have found the error there...

> 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);

The above Get will leave the file read pointer at the point after the
last digit of the ID number, which is at the end-of-line marker for that
line.

>          Ada.Text_IO.Get_Line (File => Import_File, Item => Temp_Record.Name, Last => Name_Length);

The above Get_Line will then read the rest of the ID line, up to the
end-of-line marker, and the result is the empty string (unless there is
something after the ID and before the end of the line).

The file read point is left at the start of the "name" line.

>          GenderType_IO.Get (File => Import_File, Item => Temp_Record.Gender);

That tries to the string "test" as a GenderType, causing the failure.

So the problem is that after reading the ID you must move the read point
to the start of the "name" line. Add a Skip_Line between the Get(ID) and
the Get_Line(Name).

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

Also try to configure your news-reader to break the lines in your
postings to some reasonable length, say 70 characters. Commenting is
difficult if you post messages with very long lines, at least in my
news-reader (Thunderbird).

-- 
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
      .      @       .

  parent reply	other threads:[~2014-03-04 21:42 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
2014-03-04 21:35     ` Laurent
2014-03-04 21:42     ` Niklas Holsti [this message]
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