comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adam@irvine.com>
Subject: Re: newline in a simple program
Date: Tue, 18 Dec 2007 08:44:57 -0800 (PST)
Date: 2007-12-18T08:44:57-08:00	[thread overview]
Message-ID: <86edc763-de4c-4c41-a530-ddd43874e559@d21g2000prf.googlegroups.com> (raw)
In-Reply-To: M1P9j.7718$wy2.3046@edtnps90

On Dec 18, 4:28 am, Paul <du...@telus.net> wrote:
> Hi, given the following two simple programs
>
> hello.adb
> -----------------------
> with ada.text_io;
> procedure hello is
> begin
> ada.text_io.put("hello");
> end hello;
> -----------------------
>
> hello.c
> -----------------------
> #include <stdio.h>
> int main()
> {
>      printf("hello");}
>
> -----------------------
>
> the result for the ada program is:
> -----------------------
> $ ./hello_in_ada
> hello
> $
> -----------------------
>
> and for the c program:
> -----------------------
> $ ./hello_in_c
> hello$
> -----------------------
>
> The c program does not print a new-line, but the ada program does.
>
> Is there any way to make the ada program not print a new-line?
>
> And is this behavior part of the Ada standard or is it just part of gnat?

Regarding whether this is part of the Ada standard or not: this
actually is not a simple yes/no question.  The Ada standard says that
when an output file is closed, the effect of New_Page takes place (A.
10.2(3)); this means that a "line terminator" is written if the
current line is not already terminated, and that a "page terminator"
is written if the current page is not already terminated.  However,
"line terminator" and "page terminator" are logical concepts, and the
standard makes clear that they don't have to be represented by actual
characters in a file.  (If, for example, an *input* text file on a
Unix-type system does not end in a newline, I believe a reasonable Ada
implementation would treat the logical end-of-file as a line
terminator, as well as a page terminator, even though there are no
bytes in the file to indicate the end of line.)

What this does mean, though, is that logically, it doesn't matter
whether the last output operation to the file is New_Line/Put_Line or
not.  Thus, in some sense, these have the same effect:

    ...
    Text_IO.Put_Line (File, "ABC");
    Text_IO.Put (File, "DEF");
    Text_IO.Close (File);

and

    ...
    Text_IO.Put_Line (File, "ABC");
    Text_IO.Put_Line (File, "DEF");
    Text_IO.Close (File);

since in the second case, the last Put_Line will output a line
terminator; and in the first case, the Close will output a line
terminator because the current line hasn't yet been terminated.

So it doesn't surprise me that it's hard to use Text_IO to write a
text file that isn't terminated by newline.  If you really want this,
you have to go "outside Ada" to tell the implementation something
about the actual bytes you want put into the file (Ada doesn't define
this).  Some Ada implementations may provide this ability in the Form
parameter when you open the file.

Hope this helps,
                              -- Adam




  parent reply	other threads:[~2007-12-18 16:44 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-12-18 12:28 newline in a simple program Paul
2007-12-18 12:48 ` petter_fryklund
2007-12-18 13:11 ` Dmitry A. Kazakov
2007-12-18 13:45   ` petter_fryklund
2007-12-18 13:59     ` Dmitry A. Kazakov
2007-12-18 14:13 ` Brian Drummond
2007-12-18 14:49   ` petter_fryklund
2007-12-18 19:40     ` Jeffrey R. Carter
2007-12-18 16:44 ` Adam Beneschan [this message]
2007-12-19  4:35 ` anon
2007-12-19 11:43 ` Paul
replies disabled

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