digitalmars.D.learn - Force write to write to console
- Brother Bill (9/9) Dec 06 ```
- Alexandru Ermicioi (2/11) Dec 06 make sure you flush stdout buffer
- H. S. Teoh (9/21) Dec 06 I.e.:
- Andy Valencia (9/11) Dec 06 Of course, if this is debugging or such output which is
- Peter C (24/33) Dec 07 // A newline will force a flush: write("foo\n"); // appears
```
write("foo"); // writes to the console, but it doesn't display
yet.
writeln; // Now it displays, with a new line.
```
How to force writing to the console after ```write("foo");```
I am doing this for a tutorial video, and want the
```write("foo");```
to immediately write to the console.
Dec 06
On Saturday, 6 December 2025 at 21:25:27 UTC, Brother Bill wrote:
```
write("foo"); // writes to the console, but it doesn't display
yet.
writeln; // Now it displays, with a new line.
```
How to force writing to the console after ```write("foo");```
I am doing this for a tutorial video, and want the
```write("foo");```
to immediately write to the console.
make sure you flush stdout buffer
Dec 06
On Sat, Dec 06, 2025 at 11:07:30PM +0000, Alexandru Ermicioi via Digitalmars-d-learn wrote:On Saturday, 6 December 2025 at 21:25:27 UTC, Brother Bill wrote:I.e.: ``` write("something"); stdout.flush; ``` T -- MICROSOFT = Most Intelligent Customers Realize Our Software Only Fools Teenagers``` write("foo"); // writes to the console, but it doesn't display yet. writeln; // Now it displays, with a new line. ``` How to force writing to the console after ```write("foo");``` I am doing this for a tutorial video, and want the ```write("foo");``` to immediately write to the console.make sure you flush stdout buffer
Dec 06
On Saturday, 6 December 2025 at 23:51:04 UTC, H. S. Teoh wrote:
write("something");
stdout.flush;
Of course, if this is debugging or such output which is
orthogonal to the main code's output, stderr is a good choice, as
it's unbuffered.
```d
import std.stdio : stdout;
stderr.write("Hello, world.");
```
(The output comes immediately with the call.)
Dec 06
On Saturday, 6 December 2025 at 21:25:27 UTC, Brother Bill wrote:
```
write("foo"); // writes to the console, but it doesn't display
yet.
writeln; // Now it displays, with a new line.
```
How to force writing to the console after ```write("foo");```
I am doing this for a tutorial video, and want the
```write("foo");```
to immediately write to the console.
// A newline will force a flush: write("foo\n"); // appears
immediately
// You could also do it in an 'unsafe' C way, by disabling
buffering.
// D makes it ever so easy to simply drop into C mode code
// Or make a call to stdout.flush() - as others have noted
already.
module myModule;
import std.stdio : write;
import core.thread;
import core.stdc.stdio;
safe:
private:
trusted void main()
{
// Disable buffering
setvbuf(core.stdc.stdio.stdout, null, _IONBF, 0); // NOTE:
Calls into C are not safe
write("foo"); // appears immediately
Thread.sleep(3.seconds);
// NOTE: Toggling buffering modes mid-program is unwise ;-)
// So pick one mode at startup, or use stdout.flush()
}
Dec 07









Andy Valencia <dont spam.me> 