comp.lang.ada
 help / color / mirror / Atom feed
* newline in a simple program
@ 2007-12-18 12:28 Paul
  2007-12-18 12:48 ` petter_fryklund
                   ` (5 more replies)
  0 siblings, 6 replies; 11+ messages in thread
From: Paul @ 2007-12-18 12:28 UTC (permalink / raw)


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?

I looked for answers, but found none.

Thanks,

Paul Zacharzewski



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

* Re: newline in a simple program
  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
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 11+ messages in thread
From: petter_fryklund @ 2007-12-18 12:48 UTC (permalink / raw)


Hi!

The NL is added when stdout is closed. See file a-textio.adb. You can
easily see this if you but a delay statement last in hello.adb.

Regards,
Petter



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

* Re: newline in a simple program
  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 14:13 ` Brian Drummond
                   ` (3 subsequent siblings)
  5 siblings, 1 reply; 11+ messages in thread
From: Dmitry A. Kazakov @ 2007-12-18 13:11 UTC (permalink / raw)


On Tue, 18 Dec 2007 12:28:28 GMT, Paul 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?
> 
> I looked for answers, but found none.

You can use stream I/O in order to make sure that Text_IO wouldn't add page
formatting you don't need:

with Ada.Text_IO.Text_Streams;

use Ada.Text_IO;
use Ada.Text_IO.Text_Streams;

procedure Hello is
begin
  String'Write (Stream (Standard_Output), "hello");
end Hello;

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



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

* Re: newline in a simple program
  2007-12-18 13:11 ` Dmitry A. Kazakov
@ 2007-12-18 13:45   ` petter_fryklund
  2007-12-18 13:59     ` Dmitry A. Kazakov
  0 siblings, 1 reply; 11+ messages in thread
From: petter_fryklund @ 2007-12-18 13:45 UTC (permalink / raw)


Hi again!

Closing stdout will still add a NL. If you really don't want that and
GNAT is your compiler, you can use GNAT.IO.

Regards,
Petter



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

* Re: newline in a simple program
  2007-12-18 13:45   ` petter_fryklund
@ 2007-12-18 13:59     ` Dmitry A. Kazakov
  0 siblings, 0 replies; 11+ messages in thread
From: Dmitry A. Kazakov @ 2007-12-18 13:59 UTC (permalink / raw)


On Tue, 18 Dec 2007 05:45:10 -0800 (PST), petter_fryklund@hotmail.com
wrote:

> Closing stdout will still add a NL. If you really don't want that and
> GNAT is your compiler, you can use GNAT.IO.

I don't know it. Nevertheless, under Linux it works exactly as the OP
wished:

$ ./hello
hello$ 

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



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

* Re: newline in a simple program
  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 14:13 ` Brian Drummond
  2007-12-18 14:49   ` petter_fryklund
  2007-12-18 16:44 ` Adam Beneschan
                   ` (2 subsequent siblings)
  5 siblings, 1 reply; 11+ messages in thread
From: Brian Drummond @ 2007-12-18 14:13 UTC (permalink / raw)


On Tue, 18 Dec 2007 12:28:28 GMT, Paul <duneo@telus.net> wrote:

>Hi, given the following two simple programs
>
>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?

Neither; it's part of the ada.text_io library.

It should be possible to find documentation on ada.text_io, or read
ada.text_io.ads itself for more information.

If that has no way to accomplish what you need, then you simply need a
different library; and that's where Dmitry's excellent answer comes in.

- Brian



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

* Re: newline in a simple program
  2007-12-18 14:13 ` Brian Drummond
@ 2007-12-18 14:49   ` petter_fryklund
  2007-12-18 19:40     ` Jeffrey R. Carter
  0 siblings, 1 reply; 11+ messages in thread
From: petter_fryklund @ 2007-12-18 14:49 UTC (permalink / raw)


Hi again!

I tried GNAT 6.1.0w on Solaris. Got NL.

Regards,
Petter



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

* Re: newline in a simple program
  2007-12-18 12:28 newline in a simple program Paul
                   ` (2 preceding siblings ...)
  2007-12-18 14:13 ` Brian Drummond
@ 2007-12-18 16:44 ` Adam Beneschan
  2007-12-19  4:35 ` anon
  2007-12-19 11:43 ` Paul
  5 siblings, 0 replies; 11+ messages in thread
From: Adam Beneschan @ 2007-12-18 16:44 UTC (permalink / raw)


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




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

* Re: newline in a simple program
  2007-12-18 14:49   ` petter_fryklund
@ 2007-12-18 19:40     ` Jeffrey R. Carter
  0 siblings, 0 replies; 11+ messages in thread
From: Jeffrey R. Carter @ 2007-12-18 19:40 UTC (permalink / raw)


petter_fryklund@hotmail.com wrote:
> 
> I tried GNAT 6.1.0w on Solaris. Got NL.

GNAT 6.1.0w on Linux does not output a line terminator with Kazakov's program. 
This may be shell- or OS-specific, taking pains to always output the prompt on a 
new line.

-- 
Jeff Carter
"From this day on, the official language of San Marcos will be Swedish."
Bananas
28



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

* Re: newline in a simple program
  2007-12-18 12:28 newline in a simple program Paul
                   ` (3 preceding siblings ...)
  2007-12-18 16:44 ` Adam Beneschan
@ 2007-12-19  4:35 ` anon
  2007-12-19 11:43 ` Paul
  5 siblings, 0 replies; 11+ messages in thread
From: anon @ 2007-12-19  4:35 UTC (permalink / raw)


--
--
--  GNAT.IO and "System.IO"  uses the "printf" 
--
--  "Ada.Text_IO" uses "fprintf" because it is based on file stucture.
--                Since this package uses files it must closed and 
--                the core library routine adds a NL character during
--                the closing process.
--
with Ada.Text_IO ;
with GNAT.IO ;
with System.IO ;

procedure y is

begin

  Ada.Text_IO.Put ( "Testing NL" ) ;
  GNAT.IO.Put ( "Testing NL" ) ;
  System.IO.Put ( "Testing NL" ) ;
  Ada.Text_IO.Put_Line ( "DONE" ) ;

end y ;

In <M1P9j.7718$wy2.3046@edtnps90>, Paul <duneo@telus.net> writes:
>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?
>
>I looked for answers, but found none.
>
>Thanks,
>
>Paul Zacharzewski




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

* Re: newline in a simple program
  2007-12-18 12:28 newline in a simple program Paul
                   ` (4 preceding siblings ...)
  2007-12-19  4:35 ` anon
@ 2007-12-19 11:43 ` Paul
  5 siblings, 0 replies; 11+ messages in thread
From: Paul @ 2007-12-19 11:43 UTC (permalink / raw)


Thanks for all the replies.

Esp. Dimitri, and Adams insight.

I think Streams is exactly what I was looking for.

Paul.



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

end of thread, other threads:[~2007-12-19 11:43 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
2007-12-19  4:35 ` anon
2007-12-19 11:43 ` Paul

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