September 02, 2008 Re: Delay function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Brendan | Reply to Brendan,
> Jarrett Billingsley Wrote:
>
>> On Tue, Sep 2, 2008 at 1:37 PM, Chris R. Miller <
>> lordSaurontheGreat@gmail.com> wrote:
>>> Brendan wrote:
>>>
>>>> Jarrett Billingsley Wrote:
>>>>
>>>>> </blockquote></div><br>If you're expecting it to pause after each
>>>>>
>>> character.. you're missing braces around the body of the for loop,
>>> this isn't Python ;)<br></div>
>>>
>>>> Oooooh, what a foolish mistake! How could I forget that? Thanks for
>>>>
>>> pointing it out.
>>>
>>>> But the result isn't fruitful, though. When I run it, all I get is
>>>> an
>>>>
>>> empty screen, then after many seconds the whole string is displayed
>>> at once (not to mention I set the sleep time to 0.1 seconds). I'm
>>> probably missing something. Any idea?
>>>
>>> Flush the output?
>>>
>> Totally.
>>
>> foreach(ch; s) { write(ch); fflush(stdout); system("pause 0.2"); }
>>
> Thanks! With a little quick tweaking it worked. But, even though I see
> the effective result, I don't entirely understand what it means "to
> flush" the output. I've never heard of this concept before and Google
> doesn't seem to return any useful answers. Could you explain?
>
The act of making the screen show text is rather costly in time, so generally libs buffer your output, often until a newline or some preset amount. Flush just forces a buffer flush and the resultant output.
|
September 02, 2008 Re: Delay function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Brendan | On Wed, 03 Sep 2008 00:24:02 +0400, Brendan <brenzie@gmail.com> wrote:
> Jarrett Billingsley Wrote:
>
>> On Tue, Sep 2, 2008 at 1:37 PM, Chris R. Miller <
>> lordSaurontheGreat@gmail.com> wrote:
>>
>> > Brendan wrote:
>> > > Jarrett Billingsley Wrote:
>> > >
>> > >
>> > >> </blockquote></div><br>If you're expecting it to pause after each
>> > character.. you're missing braces around the body of the for loop,
>> this
>> > isn't Python ;)<br></div>
>> > >>
>> > >
>> > > Oooooh, what a foolish mistake! How could I forget that? Thanks for
>> > pointing it out.
>> > >
>> > > But the result isn't fruitful, though. When I run it, all I get is
>> an
>> > empty screen, then after many seconds the whole string is displayed
>> at once
>> > (not to mention I set the sleep time to 0.1 seconds). I'm probably
>> missing
>> > something. Any idea?
>> >
>> > Flush the output?
>> >
>> >
>> Totally.
>>
>> foreach(ch; s) { write(ch); fflush(stdout); system("pause 0.2"); }
>
> Thanks! With a little quick tweaking it worked. But, even though I see the effective result, I don't entirely understand what it means "to flush" the output. I've never heard of this concept before and Google doesn't seem to return any useful answers. Could you explain?
Google for "buffering". When you print something, it doesn't get displayed immediately. Instead, the string is copied to some internal buffer and when it gets full (or close to full) it is then flushed, i.e. its contents copied to the console and buffer size reset. Something like this:
char[] buffer;
void writefln(char[] str)
{
buffer ~= str;
if (buffer.length > 1024) {
doTheRealPrinting(buffer);
buffer.length = 0;
}
}
|
September 02, 2008 Re: Delay function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Denis Koroskin | Denis Koroskin Wrote:
> Google for "buffering". When you print something, it doesn't get displayed immediately. Instead, the string is copied to some internal buffer and when it gets full (or close to full) it is then flushed, i.e. its contents copied to the console and buffer size reset. Something like this:
>
> char[] buffer;
>
> void writefln(char[] str)
> {
> buffer ~= str;
> if (buffer.length > 1024) {
> doTheRealPrinting(buffer);
> buffer.length = 0;
> }
> }
Ahhh, a buffer!
So basically, in the correct piece of code (the scroll function) a few messages back, in the for loop, the buffer is constantly unloading the characters from itself? It is so beautiful. Thanks :)
|
September 02, 2008 Re: Delay function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | BCS Wrote:
> Reply to Brendan,
>
> > Jarrett Billingsley Wrote:
> >
> >> On Tue, Sep 2, 2008 at 1:37 PM, Chris R. Miller < lordSaurontheGreat@gmail.com> wrote:
> >>> Brendan wrote:
> >>>
> >>>> Jarrett Billingsley Wrote:
> >>>>
> >>>>> </blockquote></div><br>If you're expecting it to pause after each
> >>>>>
> >>> character.. you're missing braces around the body of the for loop, this isn't Python ;)<br></div>
> >>>
> >>>> Oooooh, what a foolish mistake! How could I forget that? Thanks for
> >>>>
> >>> pointing it out.
> >>>
> >>>> But the result isn't fruitful, though. When I run it, all I get is an
> >>>>
> >>> empty screen, then after many seconds the whole string is displayed at once (not to mention I set the sleep time to 0.1 seconds). I'm probably missing something. Any idea?
> >>>
> >>> Flush the output?
> >>>
> >> Totally.
> >>
> >> foreach(ch; s) { write(ch); fflush(stdout); system("pause 0.2"); }
> >>
> > Thanks! With a little quick tweaking it worked. But, even though I see the effective result, I don't entirely understand what it means "to flush" the output. I've never heard of this concept before and Google doesn't seem to return any useful answers. Could you explain?
> >
>
> The act of making the screen show text is rather costly in time, so generally libs buffer your output, often until a newline or some preset amount. Flush just forces a buffer flush and the resultant output.
>
>
Thanks for your responses, BCS. This is the first time I'm kind of communicating through a newsgroup such as this, as I'm normally used to a forum. Why are your two messages separate from the thread?
|
September 03, 2008 Re: Delay function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Brendan | Reply to Brendan,
> Thanks for your responses, BCS. This is the first time I'm kind of
> communicating through a newsgroup such as this, as I'm normally used
> to a forum. Why are your two messages separate from the thread?
>
If you are using the D web site interface, because it's broken (and has been for years). Best approach is to get a "real" newsgroup client program, thunderbird works, I use Omea by jetbrains and IIRC Outlook also does newsgroups.
|
September 03, 2008 Re: Delay function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | BCS wrote:
> Reply to Brendan,
>
>> Thanks for your responses, BCS. This is the first time I'm kind of
>> communicating through a newsgroup such as this, as I'm normally used
>> to a forum. Why are your two messages separate from the thread?
>>
>
> If you are using the D web site interface, because it's broken (and has been for years). Best approach is to get a "real" newsgroup client program, thunderbird works, I use Omea by jetbrains and IIRC Outlook also does newsgroups.
>
>
It's a lot better now, as I'm using Thunderbird. Thanks for the tip :)
|
Copyright © 1999-2021 by the D Language Foundation