digitalmars.D - writef
- Claus D. Volko (19/19) Jan 11 2009 Hi,
- Jason House (2/35) Jan 11 2009 I'm going to guess the answer is flushing. I suspect writefln will flus...
- Claus D. Volko (7/8) Jan 11 2009 Thanks for your answer. It sounds plausible to me. Do you know how to ma...
- Adam D. Ruppe (6/7) Jan 11 2009 fflush(stdout);
- Claus D. Volko (15/21) Jan 13 2009 I've tried it - it doesn't help. :(
- Steven Schveighoffer (6/30) Jan 13 2009 Try reversing the two above lines:
- Claus D. Volko (2/30) Jan 14 2009
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
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
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
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
Adam D. Ruppe Wrote:On Sun, Jan 11, 2009 at 10:15:45AM -0500, Claus D. Volko wrote: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.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;
Jan 13 2009
"Claus D. Volko" wroteAdam D. Ruppe Wrote: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.On Sun, Jan 11, 2009 at 10:15:45AM -0500, Claus D. Volko wrote: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);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 also tried calling std.stdio.fflush (stdout); to avoid calling the function from std.c.stdio - not the desired effect either.
Jan 13 2009
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: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.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;
Jan 14 2009