comp.lang.ada
 help / color / mirror / Atom feed
* Reading data from file
@ 2014-03-04  9:46 Laurent
  2014-03-04 16:17 ` adambeneschan
  2014-03-04 18:08 ` Mike H
  0 siblings, 2 replies; 20+ messages in thread
From: Laurent @ 2014-03-04  9:46 UTC (permalink / raw)


Hi

Still trying to solve an exercise. I am supposed to develop a procedure which reads the data from a txt file. My first try fails because I get an error on reading an enumeration. Unfortunately I have no idea why. 

Txt file: 

ID: 1234
Name: test
Gender: male <== reading fails with atio_enumio error
Numdepend: 1
Salary: 200.0
StartDate: 12 feb 2013

Writing this from my smartphone so I don't have a lot info available.

The EOL character: do I have to take care of those or are they automatically skipped by atio/ aitio. Get?

Or I have to check if the Name is read correctly and doesn't interfer with the enum.

Or are there more detailed examples in the ARM? 

Thanks

Laurent


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

* Re: Reading data from file
  2014-03-04  9:46 Reading data from file Laurent
@ 2014-03-04 16:17 ` adambeneschan
  2014-03-04 18:08 ` Mike H
  1 sibling, 0 replies; 20+ messages in thread
From: adambeneschan @ 2014-03-04 16:17 UTC (permalink / raw)


On Tuesday, March 4, 2014 1:46:29 AM UTC-8, Laurent wrote:
> Hi
> 
> Still trying to solve an exercise. I am supposed to develop a procedure which reads the data from a txt file. My first try fails because I get an error on reading an enumeration. Unfortunately I have no idea why. 
> 
> Txt file: 
> 
> ID: 1234
> Name: test
> Gender: male <== reading fails with atio_enumio error
> Numdepend: 1
> Salary: 200.0
> StartDate: 12 feb 2013
> 
> Writing this from my smartphone so I don't have a lot info available.
> 
> The EOL character: do I have to take care of those or are they automatically skipped by atio/ aitio. Get?

Enumeration_IO will skip any whitespace (including line terminators) at the current position *before* it tries to read the enumeration.  However, after the enumeration is read, the next character (which could be a line terminator, other whitespace, or a non-identifier character) is left in the input stream and is not skipped.

But without any code or an accurate exception message, I don't think it's possible to provide any additional help.

                                 -- Adam

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

* Re: Reading data from file
  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
  1 sibling, 1 reply; 20+ messages in thread
From: Mike H @ 2014-03-04 18:08 UTC (permalink / raw)


In message <a8343425-3e25-4592-a5f5-5395f4a70221@googlegroups.com>, 
Laurent <daemon2@internet.lu> writes
>Still trying to solve an exercise. I am supposed to develop a procedure 
>which reads the data from a txt file.

In Ada (or at least in Ada95) a piece of text is a String. A String is 
an unconstrained (i.e. variable length) array of Characters. A 
Character takes any of the 128 ASCII possible values so non printable 
characters are also visible.  Anything you can do with an array you can 
do with a string, and vice-versa. So, for each line of input I suggest 
that you read it as a String and the  parse the string in order to 
separate key word from data. Thus, in the case of your line of text

>Gender: male <== reading fails with atio_enumio error

I suggest that having read the complete line you first verify that 
characters 1..8 are indeed "Gender. " and then parse the following 
non-space text. Furthermore if, as it would appear, your enumeration 
type contains less than a handful of values you forget enumeration_io. I 
hope the following code will indicate what I mean.

with Text_IO;
procedure Laurent is

    type Gender is (male, female, cross_gender);
    subtype Some_arbitrary_range is positive range 1..10;
    -- ID_line, Name_line :  String (Some_arbitrary_range);
    Gender_line : String (Some_arbitrary_range);
    -- Numdepend_line, etc
    Line_length : Positive;
begin
    Text_IO.Get_line (Item => Gender_Line, Last => Line_Length);
    case Line_length is
       when 1..8 =>
          -- insert code to report that the line appears to be too short
          null;
       when others =>
          if Gender_Line (1..8) /= "Gender. " then
             -- insert code to report that the line appears to be ill 
formed
             null;
          else
             case Gender_Line (9) is
                when 'M' | 'm' =>
                   -- insert code here to parse ID_line (10..Line_length)
                   -- looking for "ale"
                   null;
                when 'F' | 'f' =>
                   -- insert code here to parse ID_line (10..Line_length)
                   -- looking for "emale"
                   null;
                when 'C' | 'c' =>
                   -- etc
                   null;
                when others =>
                   -- report an error
                   null;
             end case;
             null;
          end if;
       end case;

end Laurent;

-- 
Mike
Swim? Naturally at Severn Vale
<http://www.severnvalesc.org/>

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

* Re: Reading data from file
  2014-03-04 18:08 ` Mike H
@ 2014-03-04 21:27   ` Laurent
  2014-03-04 21:35     ` Laurent
                       ` (2 more replies)
  0 siblings, 3 replies; 20+ messages in thread
From: Laurent @ 2014-03-04 21:27 UTC (permalink / raw)


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.

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

* Re: Reading data from file
  2014-03-04 21:27   ` Laurent
@ 2014-03-04 21:35     ` Laurent
  2014-03-04 21:42     ` Niklas Holsti
  2014-03-05 13:59     ` Mike H
  2 siblings, 0 replies; 20+ messages in thread
From: Laurent @ 2014-03-04 21:35 UTC (permalink / raw)


Am Dienstag, 4. März 2014 22:27:25 UTC+1 schrieb Laurent:
> Hi

> 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. 

> @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.

It is indeed the name string which bombs my program. Commented this line out and removed it from the txt file and everything works :( 

Thanks

Laurent


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

* Re: Reading data from file
  2014-03-04 21:27   ` Laurent
  2014-03-04 21:35     ` Laurent
@ 2014-03-04 21:42     ` Niklas Holsti
  2014-03-05 13:59     ` Mike H
  2 siblings, 0 replies; 20+ messages in thread
From: Niklas Holsti @ 2014-03-04 21:42 UTC (permalink / raw)


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
      .      @       .

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

* Re: Reading data from file
  2014-03-04 21:27   ` Laurent
  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
  2 siblings, 1 reply; 20+ messages in thread
From: Mike H @ 2014-03-05 13:59 UTC (permalink / raw)


In message <9b780534-04fb-43c8-b8de-1610421c471d@googlegroups.com>, 
Laurent <daemon2@internet.lu> writes
>Here is my code. It compiles but it isn't complete.

 From what I see of your code so far, it is going to fail if any of the 
data in the input file is corrupt. Do not take that as a criticism 
because, depending upon the requirements as set out in the exercise you 
are attempting, it may be quite acceptable. In which case you have 
nothing to worry about.

But in the real world corrupt data is an inevitability. If you don't 
believe me, try putting a non-numeric character in the "ID" line. By 
it's very nature, corrupt data is chaotic. Trapping and reporting such 
errors and then recovering from that situation and dealing with further 
data (whether corrupt or not) takes a certain amount of skill. It also 
involves writing a considerable amount of code, which by definition, one 
hope will never need to be executed.

-- 
Time flies like an arrow. Fruit flies like a banana.
Mike

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

* Re: Reading data from file
  2014-03-05 13:59     ` Mike H
@ 2014-03-05 20:33       ` Laurent
  2014-03-05 21:00         ` Jeffrey Carter
  0 siblings, 1 reply; 20+ messages in thread
From: Laurent @ 2014-03-05 20:33 UTC (permalink / raw)


Am Dienstag, 4. März 2014 22:42:37 UTC+1 schrieb Niklas Holsti:

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

I don't use a news-reader so difficult to configure.
I will try to limit my postings to 1/2 the width.

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

The Skip_Line works only if I use it together with:

Ada.Text_IO.Get (File => Import_File, Item => Temp_Record.Name);

and this only if I put spaces after "test" to a total length of 30 chars.
I suppose the spaces are needed because the name is defined as:

   MaxName : constant Positive := 30;
   subtype NameType is String (1 .. MaxName);

Get_Line w/ or w/o Skip_Line fails with the same error 
independently of the number of spaces behind the name.

Even tried this:

declare 
       S : String := Ada.Text_IO.Get_Line(File => Import_File);
        begin
            Temp_Record.Name (1 .. S'Length) := S;
         end;

Works w/o the (File => Import_File). Then it waits for
keyboard input and everything is fine. If it tries to read
from file then not.

Until now I like Ada very much but this odd behavior
of Ada.Text_IO.Get/Get_Line drives me crazy.

The integer/enumeration getters aren't so picky.

@ Mike H

>If you don't believe me, try putting a non-numeric 
>character in the "ID" line.

Doesn't even need to be non-numeric. ID needs to be
in range 1111..9999.

The Insert procedure from keyboard is robust and 
won't continue until every field is entered correctly.

Just began the read from file procedure. Would be
happy if that worked. Then I will take care of robustness.

>Do not take that as a criticism ...

I'm an noob so criticism is quite welcome. I don't
earn my money as programmer and probably this
never happens so there are a lot of things I don't know. 
It will help me improve my code, style or whatever.

Thanks

Laurent


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

* Re: Reading data from file
  2014-03-05 20:33       ` Laurent
@ 2014-03-05 21:00         ` Jeffrey Carter
  2014-03-05 21:13           ` Laurent
  0 siblings, 1 reply; 20+ messages in thread
From: Jeffrey Carter @ 2014-03-05 21:00 UTC (permalink / raw)


On 03/05/2014 01:33 PM, Laurent wrote:
> Am Dienstag, 4. März 2014 22:42:37 UTC+1 schrieb Niklas Holsti:
>
>> 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).
>
> The Skip_Line works only if I use it together with:
>
> Ada.Text_IO.Get (File => Import_File, Item => Temp_Record.Name);
>
> and this only if I put spaces after "test" to a total length of 30 chars.

If you reread what Holsti wrote, and put the Skip_Line where he said to put it, 
not where you're putting it, it should work fine.

> Until now I like Ada very much but this odd behavior
> of Ada.Text_IO.Get/Get_Line drives me crazy.
>
> The integer/enumeration getters aren't so picky.

Most of us who use Ada professionally rarely use those packages for input. 
Instead, we get a line at a time using Get_Line and convert values using 'Value, 
with an exception handler to handle incorrect input. This avoids line-end 
problems such as you have encountered.

-- 
Jeff Carter
"My mind is aglow with whirling, transient nodes of
thought, careening through a cosmic vapor of invention."
Blazing Saddles
85

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

* Re: Reading data from file
  2014-03-05 21:00         ` Jeffrey Carter
@ 2014-03-05 21:13           ` Laurent
  2014-03-05 21:25             ` Niklas Holsti
  0 siblings, 1 reply; 20+ messages in thread
From: Laurent @ 2014-03-05 21:13 UTC (permalink / raw)


It is this what he meant?

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

That doesn't work.

Ada.Integer_Text_IO.Get (File => Import_File, Item => Temp_Record.ID);
Ada.Text_IO.Skip_Line;
Ada.Text_IO.Get(File => Import_File, Item => Temp_Record.Name);

Works (not forgetting to add the spaces in the txt file)

> The Skip_Line works only if I use it together with: 
> Ada.Text_IO.Get (File => Import_File, Item => Temp_Record.Name); 

Hm yes quit confuse sentence. I knew what I meant to say which doesn't
imply that that someone else does. Sry



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

* Re: Reading data from file
  2014-03-05 21:13           ` Laurent
@ 2014-03-05 21:25             ` Niklas Holsti
  2014-03-05 21:56               ` Laurent
  0 siblings, 1 reply; 20+ messages in thread
From: Niklas Holsti @ 2014-03-05 21:25 UTC (permalink / raw)


On 14-03-05 23:13 , Laurent wrote:
> It is this what he meant?
> 
> Ada.Integer_Text_IO.Get (File => Import_File, Item => Temp_Record.ID);
> Ada.Text_IO.Skip_Line;
> Ada.Text_IO.Get_Line(File => Import_File, Item => Temp_Record.Name,Last => Name_Length);
> 
> That doesn't work.

You have to do

   Ada.Text_IO.Skip_Line (Import_File);

Just Skip_Line with no parameters skips a line on Standard_Input, which
is not what you want.

I'm sorry if my suggestion was unclear.

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


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

* Re: Reading data from file
  2014-03-05 21:25             ` Niklas Holsti
@ 2014-03-05 21:56               ` Laurent
  2014-03-06  8:35                 ` Dmitry A. Kazakov
  0 siblings, 1 reply; 20+ messages in thread
From: Laurent @ 2014-03-05 21:56 UTC (permalink / raw)


Ok now it works. 

>I'm sorry if my suggestion was unclear. 

Hm no it is more the problem that there are
things which are supposed to be know. So if
you explain something to someone else you just
explain what you consider to be important.

Happens to me too. 

Could have stumbled over this option of Skip_Line
if I didn't use the Alias feature of GPS.

atiosl then ctrl-O and we have an Ada.Text_IO.Skip_Line;

Thanks for your help/patience

Laurent

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

* Re: Reading data from file
  2014-03-05 21:56               ` Laurent
@ 2014-03-06  8:35                 ` Dmitry A. Kazakov
  2014-03-07 21:55                   ` Laurent
  0 siblings, 1 reply; 20+ messages in thread
From: Dmitry A. Kazakov @ 2014-03-06  8:35 UTC (permalink / raw)


On Wed, 5 Mar 2014 13:56:48 -0800 (PST), Laurent wrote:

> Thanks for your help/patience

You are welcome.

BTW, there are John Woodruff's I/O packages for simple user input of
scientific data much akin to yours. See

http://www.dmitry-kazakov.de/ada/Numeric-Name-IO.htm

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

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

* Re: Reading data from file
  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  9:00                     ` Simon Wright
  0 siblings, 2 replies; 20+ messages in thread
From: Laurent @ 2014-03-07 21:55 UTC (permalink / raw)


Hi

While building my procedure to read data from
a text file I had a few endless loops which blocked
GPS completely. Got spinning ball of death and I had
to force quit GPS. So I had no possibility to see my
text to find out where it loops.

I tried to open my binary app which displayed the text.
But when it arrives at the place where it should read the
text file it raises:

raised ADA.IO_EXCEPTIONS.NAME_ERROR : Datafile.txt: No such file or directory

Now that I have found the error of the endless loops 
and everything works as it should in GPS.

But the application still raises this error.

The txt file is in the folder with the dab/adses, the
application is in the build folder:

Source Folder/Datafile.txt
Source Folder/Build/My_Application

Quite useless application if it doesn't
work without GPS.

How can I solve this?

Thanks

Laurent

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

* Re: Reading data from file
  2014-03-07 21:55                   ` Laurent
@ 2014-03-08  6:56                     ` Dmitry A. Kazakov
  2014-03-08 21:21                       ` Laurent
  2014-03-08  9:00                     ` Simon Wright
  1 sibling, 1 reply; 20+ messages in thread
From: Dmitry A. Kazakov @ 2014-03-08  6:56 UTC (permalink / raw)


On Fri, 7 Mar 2014 13:55:41 -0800 (PST), Laurent wrote:

> While building my procedure to read data from
> a text file I had a few endless loops which blocked
> GPS completely. Got spinning ball of death and I had
> to force quit GPS. So I had no possibility to see my
> text to find out where it loops.

I suppose it is under Windows and you doing much output. That is not
entirely GPS' fault, it is the GTK text view widget GPS uses to show the
program's output. Since GTK is single-threaded and event-driven a steady
flow of output from an infinite loop chokes all GPS.

But you don't need to kill the GPS, you do your program instead from the
task manager. That will cut flooding the messages loop and GPS will respond
again.

> I tried to open my binary app which displayed the text.
> But when it arrives at the place where it should read the
> text file it raises:
> 
> raised ADA.IO_EXCEPTIONS.NAME_ERROR : Datafile.txt: No such file or directory
> 
> Now that I have found the error of the endless loops 
> and everything works as it should in GPS.
> 
> But the application still raises this error.
> 
> The txt file is in the folder with the dab/adses, the
> application is in the build folder:
> 
> Source Folder/Datafile.txt
> Source Folder/Build/My_Application
> 
> Quite useless application if it doesn't
> work without GPS.
> 
> How can I solve this?

By passing a correct file name to your application, of course. If the
application execution folder and the file folder are different you must use
the full path.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de


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

* Re: Reading data from file
  2014-03-07 21:55                   ` Laurent
  2014-03-08  6:56                     ` Dmitry A. Kazakov
@ 2014-03-08  9:00                     ` Simon Wright
  1 sibling, 0 replies; 20+ messages in thread
From: Simon Wright @ 2014-03-08  9:00 UTC (permalink / raw)


Laurent <daemon2@internet.lu> writes:

> The txt file is in the folder with the dab/adses, the
> application is in the build folder:
>
> Source Folder/Datafile.txt
> Source Folder/Build/My_Application

If the file name you give to Open is just "Datafile.txt", you need to
call your program from the directory where Datafile.txt is.

$ cd "Source Folder"
$ Build/My_Application

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

* Re: Reading data from file
  2014-03-08  6:56                     ` Dmitry A. Kazakov
@ 2014-03-08 21:21                       ` Laurent
  2014-03-09 22:39                         ` Laurent
  0 siblings, 1 reply; 20+ messages in thread
From: Laurent @ 2014-03-08 21:21 UTC (permalink / raw)


Hi

>I suppose it is under Windows and you doing much output. 

No MacOS.

Yes there was much output because
I have added a few puts to see
which loop was posing problem.

Stupid error. exit statement before the procedure
which updates the condition.

GPS didn't print anything but running
the binary did until some modification.

 Don't know if this is mac or shell specific
but I have to use

$ cd "Source Folder" 
$ ./Build/My_Application 

Thanks

Laurent

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

* Re: Reading data from file
  2014-03-08 21:21                       ` Laurent
@ 2014-03-09 22:39                         ` Laurent
  2014-03-10  2:42                           ` Jeffrey Carter
  0 siblings, 1 reply; 20+ messages in thread
From: Laurent @ 2014-03-09 22:39 UTC (permalink / raw)


Hi

I have a quite strange error/behavior of my code
which I don't understand (the error):

if this Debugging_Enabled     : Boolean := True; then
the procedure below reads the 3 records
from my text file. 2 are inserted into the
database, the 3rd is not because same
ID as the 1st.

If Debugging_Enabled     : Boolean := False; then
only the 1st record is read. Same txt file.
I have localized the error:

if  Debugging_Enabled then
     Ada.Text_IO.Put_Line ("Database Manager: read file loop");
     Ada.Text_IO.Put_Line ("Status: End of file: " & Boolean'Image (End_Of_File));
     Ada.Text_IO.Put_Line ("Status: End of record: " & Boolean'Image (End_Of_Record));
     Ada.Text_IO.Put_Line ("Successfully integrated: " & Natural'Image (Successful_Integration));
     Ada.Text_IO.Put_Line ("Failed integration(s) :" & Natural'Image (Failed_Integration));
     Ada.Text_IO.New_Line;
end if;

If this is executed everything is fine.
I had commented out everything
except Ada.Text_IO.New_Line; and the it worked.
With just a null statement it fails.?!

No idea what is wrong. I don't see why this happens.

https://github.com/Chutulu/Chapter-11.git

Is the database_manager.adb

Will not compile because the screen.ads/adb is missing.
just needs to be commented out.

Thanks

Laurent

/snip

declare

            End_Of_Record : Boolean := False; -- Import_File is not empty
            End_Of_File   : Boolean := False;    -- Import_File is not empty
            Debugging_Enabled     : Boolean := True; -- hope we don't need this
            
            Import_File           : Ada.Text_IO.File_Type;
            
            Successful_Integration : Natural := 0;
            Failed_Integration     : Natural := 0;
            Record_Counter        : Natural := 0;

         begin -- declare
            
            -- 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_From_File : loop

               Screen.ClearScreen;
               Screen.MoveCursor (Row => 3, Column => 5);
               Ada.Text_IO.Put_Line ("Read data from file");
               Ada.Text_IO.New_Line;

               Read_File : loop                  
                  
                  if  Debugging_Enabled then
                     Ada.Text_IO.Put_Line ("Database Manager: read file loop");
                     Ada.Text_IO.Put_Line ("Status: End of file: " & Boolean'Image (End_Of_File));
                     Ada.Text_IO.Put_Line ("Status: End of record: " & Boolean'Image (End_Of_Record));
                     Ada.Text_IO.Put_Line ("Successfully integrated: " & Natural'Image (Successful_Integration));
                     Ada.Text_IO.Put_Line ("Failed integration(s) :" & Natural'Image (Failed_Integration));
                     Ada.Text_IO.New_Line;
                  end if;
                  
                  exit when End_Of_File;
                  
                  Read_Record : loop

                     --if Debugging_Enabled then
                        --Ada.Text_IO.Put_Line ("Database Manager: read record loop");
                        --Ada.Text_IO.Put_Line ("Status: End of file: " & Boolean'Image (End_Of_File));
                        --Ada.Text_IO.Put_Line ("Status: End of record: " & Boolean'Image (End_Of_Record));
                        --Ada.Text_IO.Put_Line ("Successfully integrated: " & Natural'Image     (Successful_Integration));
                        --Ada.Text_IO.Put_Line ("Failed integration(s) :" & Natural'Image (Failed_Integration));
                        --Ada.Text_IO.New_Line;
                     --end if;

                     Employees.IO.Read_Employee_From_File (File => Import_File,
                                                           Item          => E,
                                                           End_Of_Record => End_Of_Record,
                                                           EOF           => End_Of_File);

                     if not End_Of_Record then
                        Database.Insert (E => E, Success => Success);
                        Record_Counter := Record_Counter + 1;
                        if Success then
                           Successful_Integration := Successful_Integration + 1;
                        else
                           Failed_Integration := Failed_Integration + 1;
                           Ada.Text_IO.Put_Line("Record failed to be read:" & Natural'Image(Record_Counter));
                        end if;
                     end if;

                     exit when End_Of_Record;

                  end loop Read_Record;
               end loop Read_File;

               Ada.Text_IO.New_Line;
               Ada.Text_IO.Put_Line ("Succesfully integrated:" & Natural'Image(Successful_Integration));
               Ada.Text_IO.Put_Line ("Failed integration(s):" & Natural'Image (Failed_Integration));
               Ada.Text_IO.Put_Line("Total number of records in file:" & Natural'Image(Record_Counter));
               Ada.Text_IO.New_Line;
               Ada.Text_IO.Put (Item => "Enter Q to go back to main menu: ");
               Command_IO.Get (Item => MenuSelection);

               exit when MenuSelection = Q;
               Ada.Text_IO.New_Line;

            end loop Read_From_File;
         end; -- declare Read_From_File
snip/

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

* Re: Reading data from file
  2014-03-09 22:39                         ` Laurent
@ 2014-03-10  2:42                           ` Jeffrey Carter
  2014-03-11 20:54                             ` Laurent
  0 siblings, 1 reply; 20+ messages in thread
From: Jeffrey Carter @ 2014-03-10  2:42 UTC (permalink / raw)


On 03/09/2014 03:39 PM, Laurent wrote:
>
>              Read_From_File : loop
>                 Read_File : loop
>                    Read_Record : loop

I don't know, but I suspect it has something to do with having 3 loops when you 
only need 1:

    Ada.Text_IO.Open (...);

    Read_All_Records : loop
       Employees.IO.Read_Employee_From_File (...);

       exit Read_All_Records when End_Of_File or End_Of_Record;

       Database.Insert (...);
       ...
    end loop Read_All_Records;

    Ada.Text_IO.Close (...); -- Don't forget this.

-- 
Jeff Carter
"Why, the Mayflower was full of Fireflies, and a few
horseflies, too. The Fireflies were on the upper deck,
and the horseflies were on the Fireflies."
Duck Soup
95

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

* Re: Reading data from file
  2014-03-10  2:42                           ` Jeffrey Carter
@ 2014-03-11 20:54                             ` Laurent
  0 siblings, 0 replies; 20+ messages in thread
From: Laurent @ 2014-03-11 20:54 UTC (permalink / raw)


Hi

Now it works but I still don't have
any idea what exactly was wrong.

Was still a good idea to simplify.

Thanks

Laurent

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

end of thread, other threads:[~2014-03-11 20:54 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
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

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