View mode: basic / threaded / horizontal-split · Log in · Help
January 11, 2009
writef
Hi,

I'm currently writing a D tutorial for people new to programming, and I've experienced some strange behavior:

The following program works as intended:

// The "Hello World!" program

import std.stdio;
import std.c.stdio;

void main ()
{
 int i;                                    // Variable definition
 i = 200;                                  // Assignment
 writefln ("Hello World!");                // Function call
 writefln ("The value of i is ", i, ".");  // Function call
 getch ();                                 // Function call
}

But if I replace

writefln ("The value of i is ", i, ".");

with

writef ("The value of i is ", i, ".");

the output of "The value of i is 200." happens only after the keypress. Unfortunately, I haven't been able to find any documents about writef on the Net which could have explained why it's like this, or whether it's a bug. Therefore I'm posting my question to this newsgroup: Why?
January 11, 2009
Re: writef
Claus D.  Volko wrote:

> Hi,
> 
> I'm currently writing a D tutorial for people new to programming, and I've
> experienced some strange behavior:
> 
> The following program works as intended:
> 
> // The "Hello World!" program
> 
> import std.stdio;
> import std.c.stdio;
> 
> void main ()
> {
>   int i;                                    // Variable definition
>   i = 200;                                  // Assignment
>   writefln ("Hello World!");                // Function call
>   writefln ("The value of i is ", i, ".");  // Function call
>   getch ();                                 // Function call
> }
> 
> But if I replace
> 
> writefln ("The value of i is ", i, ".");
> 
> with
> 
> writef ("The value of i is ", i, ".");
> 
> the output of "The value of i is 200." happens only after the keypress.
> Unfortunately, I haven't been able to find any documents about writef on
> the Net which could have explained why it's like this, or whether it's a
> bug. Therefore I'm posting my question to this newsgroup: Why?

I'm going to guess the answer is flushing.  I suspect writefln will flush the output following the implied newline, but writef won't.  This is similar to most console output libraries I use.  For example, C++ uses std::endl for a flushed newline and "\n" for just a newline without flushing.
January 11, 2009
Re: writef
Thanks for your answer. It sounds plausible to me. Do you know how to manually cause a flush? I've found this code snippet:

import std.cstream;

...

dout.writefln("Hello");
dout.flush();

Using doubt.writef and dout.flush, it works as intended. But can it also be done without importing std.cstream? There doesn't seem to be a function flush in std.stdio.

Jason House Wrote:
> I'm going to guess the answer is flushing.  I suspect writefln will flush the output following the implied newline, but writef won't.  This is similar to most console output libraries I use.  For example, C++ uses std::endl for a flushed newline and "\n" for just a newline without flushing.
January 11, 2009
Re: writef
On Sun, Jan 11, 2009 at 10:15:45AM -0500, Claus D. Volko wrote:
> Thanks for your answer. It sounds plausible to me. Do you know how to manually cause a flush? I've found this code snippet:

fflush(stdout);

That should do it and is imported in std.stdio;

-- 
Adam D. Ruppe
http://arsdnet.net
January 13, 2009
Re: writef
Adam D. Ruppe Wrote:

> On Sun, Jan 11, 2009 at 10:15:45AM -0500, Claus D. Volko wrote:
> > Thanks for your answer. It sounds plausible to me. Do you know how to manually cause a flush? I've found this code snippet:
> 
> fflush(stdout);
> 
> That should do it and is imported in std.stdio;

I've tried it - it doesn't help. :(

// The "Hello World!" program

import std.stdio;
import std.c.stdio;

void main ()
{
 int i;                                    // Variable definition
 i = 200;                                  // Assignment
 writefln ("Hello World!");                // Function call
 writef ("The value of i is ", i, ".");    // Function call
 getch ();                                 // Function call
 fflush (stdout);
}

I've also tried calling std.stdio.fflush (stdout); to avoid calling the function from std.c.stdio - not the desired effect either.
January 13, 2009
Re: writef
"Claus D. Volko" wrote
> Adam D. Ruppe Wrote:
>
>> On Sun, Jan 11, 2009 at 10:15:45AM -0500, Claus D. Volko wrote:
>> > Thanks for your answer. It sounds plausible to me. Do you know how to 
>> > manually cause a flush? I've found this code snippet:
>>
>> fflush(stdout);
>>
>> That should do it and is imported in std.stdio;
>
> I've tried it - it doesn't help. :(
>
> // The "Hello World!" program
>
> import std.stdio;
> import std.c.stdio;
>
> void main ()
> {
>  int i;                                    // Variable definition
>  i = 200;                                  // Assignment
>  writefln ("Hello World!");                // Function call
>  writef ("The value of i is ", i, ".");    // Function call
>  getch ();                                 // Function call
>  fflush (stdout);

Try reversing the two above lines:
fflush(stdout);
getch();

What you are doing is waiting for the input and then flushing stdout --  
exactly the same as what you had originally.

> }
>
> I've also tried calling std.stdio.fflush (stdout); to avoid calling the 
> function from std.c.stdio - not the desired effect either.
>
January 14, 2009
Re: writef
Of course the line fflush (stdout); must be written before getch(); - then it works. Great!

Claus D. Volko Wrote:

> Adam D. Ruppe Wrote:
> 
> > On Sun, Jan 11, 2009 at 10:15:45AM -0500, Claus D. Volko wrote:
> > > Thanks for your answer. It sounds plausible to me. Do you know how to manually cause a flush? I've found this code snippet:
> > 
> > fflush(stdout);
> > 
> > That should do it and is imported in std.stdio;
> 
> I've tried it - it doesn't help. :(
> 
> // The "Hello World!" program
> 
> import std.stdio;
> import std.c.stdio;
> 
> void main ()
> {
>   int i;                                    // Variable definition
>   i = 200;                                  // Assignment
>   writefln ("Hello World!");                // Function call
>   writef ("The value of i is ", i, ".");    // Function call
>   getch ();                                 // Function call
>   fflush (stdout);
> }
> 
> I've also tried calling std.stdio.fflush (stdout); to avoid calling the function from std.c.stdio - not the desired effect either.
>
Top | Discussion index | About this forum | D home