2

I have the following D program:

void main( string[] args )
{
    import core.thread, std.stdio;

    for ( int i = 10; i > 0; --i )
    {
        writeln( i );
        Thread.sleep(dur!"seconds"(1) );
    }
    writeln("Bang!");
}

It counts from 10 down to 1 and then outputs "Bang!". Between each output the program waits for the duration of one second. When I run this program in Eclipse (Eclipse Platform, Version: 3.8.1, Build id: debbuild) with the DDT plugin (Version: 0.8.1.v201309231) then the console output is as expected, but it apppears only until after the program has finished running.

When I start the same program from the console, then the program behaves as it should.

How can I fix that?

1
  • 1
    try putting stdout.flush(); before the sleep. not sure if eclipse will flush too, but worth a try Commented Oct 21, 2013 at 15:10

1 Answer 1

7

Put stdout.flush() before the sleep line. stdout is buffered, so flush ensures it actually gets out to the device instead of waiting in the buffer.

2
  • 1
    I'm not entirely sure what's going on here, but I think the line buffering only happens if the receiving end is a regular terminal. Otherwise, it gets the same buffering as any other file (because it likely is any other file). With Eclipse, again I'm guessing, it is probably talking to the eclipse process through a pipe, and that isn't registering as an interactive terminal so it goes back to full buffering. Commented Oct 22, 2013 at 0:53
  • I've opened an enhancement request for this: issues.dlang.org/show_bug.cgi?id=13778 Commented Nov 26, 2014 at 12:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.