www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - stdout - autoflushing

reply "Benji" <romanbeno273 gmail.com> writes:
Hello,
in order to have correctly displayed output (before reading 
something from stdin),
I must call stdout.flush().
Sometimes, it's really annoying, especially when it is necessarry 
to call it 10 times.

For example:
write("Enter some string: ");
stdout.flush();
string a = readln();
write("And again please: ");
stdout.flush();
string b = readln();
...

Is there any way to prevent this?
Dec 03 2013
next sibling parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Tue, Dec 03, 2013 at 06:12:20PM +0100, Benji wrote:
 Hello,
 in order to have correctly displayed output (before reading
 something from stdin),
 I must call stdout.flush().
 Sometimes, it's really annoying, especially when it is necessarry to
 call it 10 times.
 
 For example:
 write("Enter some string: ");
 stdout.flush();
 string a = readln();
 write("And again please: ");
 stdout.flush();
 string b = readln();
 ...
 
 Is there any way to prevent this?
What about: void prompt(A...)(string fmt, A args) { writef(fmt, args); stdout.flush(); return readln(); } auto a = prompt("Enter your name: "); auto b = prompt("Enter your age: ").to!int; ... // etc. T -- If you think you are too small to make a difference, try sleeping in a closed room with a mosquito. -- Jan van Steenbergen
Dec 03 2013
parent "Benji" <romanbeno273 gmail.com> writes:
On Tuesday, 3 December 2013 at 17:49:32 UTC, H. S. Teoh wrote:
 On Tue, Dec 03, 2013 at 06:12:20PM +0100, Benji wrote:
 Hello,
 in order to have correctly displayed output (before reading
 something from stdin),
 I must call stdout.flush().
 Sometimes, it's really annoying, especially when it is 
 necessarry to
 call it 10 times.
 
 For example:
 write("Enter some string: ");
 stdout.flush();
 string a = readln();
 write("And again please: ");
 stdout.flush();
 string b = readln();
 ...
 
 Is there any way to prevent this?
What about: void prompt(A...)(string fmt, A args) { writef(fmt, args); stdout.flush(); return readln(); } auto a = prompt("Enter your name: "); auto b = prompt("Enter your age: ").to!int; ... // etc. T
Thanks, I didn't think about that (I'm beginner)
Dec 03 2013
prev sibling next sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 12/03/2013 09:12 AM, Benji wrote:
 Hello,
 in order to have correctly displayed output (before reading something
 from stdin),
 I must call stdout.flush().
I am surprised that you need that. What is your platform? Normally, stdin and stdout are "tied". Reading from stdin flushes stdout automatically. Ali
Dec 03 2013
parent reply "Benji" <romanbeno273 gmail.com> writes:
On Tuesday, 3 December 2013 at 19:33:47 UTC, Ali Çehreli wrote:
 On 12/03/2013 09:12 AM, Benji wrote:
 Hello,
 in order to have correctly displayed output (before reading 
 something
 from stdin),
 I must call stdout.flush().
I am surprised that you need that. What is your platform? Normally, stdin and stdout are "tied". Reading from stdin flushes stdout automatically. Ali
I am using Xubuntu, 64bit, and GDC as compiler
Dec 03 2013
next sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 12/03/2013 12:36 PM, Benji wrote:
 On Tuesday, 3 December 2013 at 19:33:47 UTC, Ali Çehreli wrote:
 On 12/03/2013 09:12 AM, Benji wrote:
 Hello,
 in order to have correctly displayed output (before reading something
 from stdin),
 I must call stdout.flush().
I am surprised that you need that. What is your platform? Normally, stdin and stdout are "tied". Reading from stdin flushes stdout automatically. Ali
I am using Xubuntu, 64bit, and GDC as compiler
I've known this to be the case for cin and cout of C++. So, I've been assuming that to be universally true. Apparently not for C and D behavior is based on C. I wish std.stdio gave us C++'s 'tie'. Ali P.S. This makes some of the examples at ddili.org incorrect as I never call flush. :-/
Dec 03 2013
parent "John Colvin" <john.loughran.colvin gmail.com> writes:
On Tuesday, 3 December 2013 at 21:23:19 UTC, Ali Çehreli wrote:
 On 12/03/2013 12:36 PM, Benji wrote:
 On Tuesday, 3 December 2013 at 19:33:47 UTC, Ali Çehreli wrote:
 On 12/03/2013 09:12 AM, Benji wrote:
 Hello,
 in order to have correctly displayed output (before reading 
 something
 from stdin),
 I must call stdout.flush().
I am surprised that you need that. What is your platform? Normally, stdin and stdout are "tied". Reading from stdin flushes stdout automatically. Ali
I am using Xubuntu, 64bit, and GDC as compiler
I've known this to be the case for cin and cout of C++. So, I've been assuming that to be universally true. Apparently not for C and D behavior is based on C. I wish std.stdio gave us C++'s 'tie'. Ali P.S. This makes some of the examples at ddili.org incorrect as I never call flush. :-/
The 'tie' is sometimes convenient, but it's not good in Unix style stdin/stout piping or similar situations with lots of simultaneous input and output.
Dec 04 2013
prev sibling next sibling parent "Jesse Phillips" <Jesse.K.Phillips+D gmail.com> writes:
On Tuesday, 3 December 2013 at 20:36:22 UTC, Benji wrote:
 On Tuesday, 3 December 2013 at 19:33:47 UTC, Ali Çehreli wrote:
 On 12/03/2013 09:12 AM, Benji wrote:
 Hello,
 in order to have correctly displayed output (before reading 
 something
 from stdin),
 I must call stdout.flush().
I am surprised that you need that. What is your platform? Normally, stdin and stdout are "tied". Reading from stdin flushes stdout automatically. Ali
I am using Xubuntu, 64bit, and GDC as compiler
I haven't seen this behavior, though I haven't used GDC (debian is close enough right). This has been how I've retrieved user data: https://github.com/JesseKPhillips/JPDLibs/blob/cmdln/cmdln/interact.d#L60
Dec 03 2013
prev sibling parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
On Tuesday, 3 December 2013 at 20:36:22 UTC, Benji wrote:
 I am using Xubuntu, 64bit, and GDC as compiler
Any IDE? I've seen ide consoles buffer differently because the runtime sees the target as a pipe instead of a user-interactive terminal.
Dec 03 2013
parent "Benji" <romanbeno273 gmail.com> writes:
On Tuesday, 3 December 2013 at 21:47:19 UTC, Adam D. Ruppe wrote:
 On Tuesday, 3 December 2013 at 20:36:22 UTC, Benji wrote:
 I am using Xubuntu, 64bit, and GDC as compiler
Any IDE? I've seen ide consoles buffer differently because the runtime sees the target as a pipe instead of a user-interactive terminal.
I'm using using Eclipse Kepler Standard(4.3). I tryied it via shell and everything worked fine also without stdout.flush().
Dec 04 2013
prev sibling parent reply Dejan Lekic <dejan.lekic gmail.com> writes:
Benji wrote:

 Hello,
 in order to have correctly displayed output (before reading
 something from stdin),
 I must call stdout.flush().
 Sometimes, it's really annoying, especially when it is necessarry
 to call it 10 times.
 
 For example:
 write("Enter some string: ");
 stdout.flush();
 string a = readln();
 write("And again please: ");
 stdout.flush();
 string b = readln();
 ...
 
 Is there any way to prevent this?
I doubt. Your IDE is buffering application's streams. -- Dejan Lekic dejan.lekic (a) gmail.com http://dejan.lekic.org
Dec 06 2013
parent "Adam D. Ruppe" <destructionator gmail.com> writes:
On Friday, 6 December 2013 at 20:39:22 UTC, Dejan Lekic wrote:
 Benji wrote:
 Is there any way to prevent this?
I doubt. Your IDE is buffering application's streams.
You know though, this happens often enough that maybe we should just throw in a stdout.flush to the global readln function. I wouldn't put it on File.readln since that's likely wrong anyway, but on the global one it is probably what people want/expect anyway.
Dec 06 2013