Thread overview | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
December 03, 2013 stdout - autoflushing | ||||
---|---|---|---|---|
| ||||
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? |
December 03, 2013 Re: stdout - autoflushing | ||||
---|---|---|---|---|
| ||||
Posted in reply to Benji | 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 |
December 03, 2013 Re: stdout - autoflushing | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | 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)
|
December 03, 2013 Re: stdout - autoflushing | ||||
---|---|---|---|---|
| ||||
Posted in reply to Benji | 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
|
December 03, 2013 Re: stdout - autoflushing | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | 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
|
December 03, 2013 Re: stdout - autoflushing | ||||
---|---|---|---|---|
| ||||
Posted in reply to Benji | 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. :-/
|
December 03, 2013 Re: stdout - autoflushing | ||||
---|---|---|---|---|
| ||||
Posted in reply to Benji | 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 |
December 03, 2013 Re: stdout - autoflushing | ||||
---|---|---|---|---|
| ||||
Posted in reply to Benji | 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.
|
December 04, 2013 Re: stdout - autoflushing | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | 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().
|
December 04, 2013 Re: stdout - autoflushing | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | 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.
|
Copyright © 1999-2021 by the D Language Foundation