comp.lang.ada
 help / color / mirror / Atom feed
From: Jeff Carter <jrcarter@acm.org>
Subject: Re: End_Of_File does not work as expected...
Date: Sat, 25 Nov 2000 05:14:39 GMT
Date: 2000-11-25T05:14:39+00:00	[thread overview]
Message-ID: <3A1F4ABC.AB68A5C2@acm.org> (raw)
In-Reply-To: rcumv8.b93.ln@127.0.0.1

Mika Fischer wrote:
> Why does the following code not work as expected?

I can't tell you, since I don't know how you expected it to work. It
probably works as I expect it to work.

> procedure Echo is
> 
> c: Character;
> 
> begin
>         while not End_Of_File loop
>                 Get(c);
>                 Put(c);
>         end loop;
> end Echo;

This code should not compile, since Get and Put are not defined. If I
assume that the source file contains

with Ada.Text_IO;
use Ada.Text_IO;

followed by the given code, it would compile. All my following
discussion is based on this assumption.

> When I compile it and invoke it like in "cat file | ./echo" it throws the
> exception END_ERROR which indicates that I tried to read from a file
> beyond the end of file.
> So why is End_Of_File false when it should be true?

Without knowing the exact contents of file, including any line and page
terminators, I cannot answer. However, if you carefully read the
descriptions of End_Of_File and Get (Character) in ARM A.10, you will
see that there are many situations in which End_Of_File is False, yet
Get (Character) will attempt to read past the end of the file. This is
probably what you are encountering. You can find the ARM at

http://www.adapower.com/rm95/index.html

> Also if I invoke the program as "./echo" and type for example "ddd" and
> press the enter key, the following appears on my screen:
> 
> --<snip>--
> ddd
> ddd
> --<snip>--
> 
> When I press Enter once more its this:
> 
> --<snip>--
> ddd
> ddd
> 
> --<snip>--
> 
> I also don't understand why it behaves as it does.

This is expected. The first line is echoed by your operating system, and
the second by your program. The second <Enter> is echoed by the
operating system but not by your program.

> As you have certainly noticed I don't really have a clue about Ada. So my
> apollogies if this question ist just too stupid :-) (And sorry for my
> English...)

Text_IO has a number of features that seem counter-intuitive to many
people when they first encounter it, especially if they have not read
the descriptions of the operations, so your question is not *too*
stupid. For example, many people assume that Get_Line always skips a
line terminator.

For a university course, it is probably better to ask your instructor
before posting here.

-- 
Jeff Carter
"I blow my nose on you."
Monty Python & the Holy Grail



  reply	other threads:[~2000-11-25  5:14 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-11-24 23:42 End_Of_File does not work as expected Mika Fischer
2000-11-25  5:14 ` Jeff Carter [this message]
2000-11-25  0:00   ` Mika Fischer
2000-11-25  0:00     ` Jeff Carter
2000-11-26  0:00 ` Mats Weber
2000-11-26  0:00   ` Jeff Carter
2000-11-26  0:00     ` Mats Weber
replies disabled

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