Discussion:
newline in a simple program
(too old to reply)
Paul
2007-12-18 12:28:28 UTC
Permalink
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
p***@hotmail.com
2007-12-18 12:48:25 UTC
Permalink
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
Dmitry A. Kazakov
2007-12-18 13:11:22 UTC
Permalink
Post by Paul
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");
}
-----------------------
-----------------------
$ ./hello_in_ada
hello
$
-----------------------
-----------------------
$ ./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
p***@hotmail.com
2007-12-18 13:45:10 UTC
Permalink
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
Dmitry A. Kazakov
2007-12-18 13:59:56 UTC
Permalink
Post by p***@hotmail.com
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
Brian Drummond
2007-12-18 14:13:51 UTC
Permalink
Post by Paul
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
p***@hotmail.com
2007-12-18 14:49:50 UTC
Permalink
Hi again!

I tried GNAT 6.1.0w on Solaris. Got NL.

Regards,
Petter
Jeffrey R. Carter
2007-12-18 19:40:14 UTC
Permalink
Post by p***@hotmail.com
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
Adam Beneschan
2007-12-18 16:44:57 UTC
Permalink
Post by Paul
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");}
-----------------------
-----------------------
$ ./hello_in_ada
hello
$
-----------------------
-----------------------
$ ./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
anon
2007-12-19 04:35:31 UTC
Permalink
--
--
-- 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 ;
Post by Paul
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");
}
-----------------------
-----------------------
$ ./hello_in_ada
hello
$
-----------------------
-----------------------
$ ./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
Paul
2007-12-19 11:43:57 UTC
Permalink
Thanks for all the replies.

Esp. Dimitri, and Adams insight.

I think Streams is exactly what I was looking for.

Paul.

Continue reading on narkive:
Loading...