www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - writef

reply Claus D. Volko <cdvolko gmx.net> writes:
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?
Jan 11 2009
parent reply Jason House <jason.james.house gmail.com> writes:
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.
Jan 11 2009
next sibling parent reply Claus D. Volko <cdvolko gmx.net> writes:
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.

Jan 11 2009
parent reply Claus D. Volko <cdvolko gmx.net> writes:
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.
Jan 13 2009
next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
"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.
 

Jan 13 2009
prev sibling parent Claus D. Volko <cdvolko gmx.net> writes:
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.

Jan 14 2009
prev sibling parent "Adam D. Ruppe" <destructionator gmail.com> writes:
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
Jan 11 2009