www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Force write to write to console

reply Brother Bill <brotherbill mail.com> writes:
```
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
next sibling parent reply Alexandru Ermicioi <alexandru.ermicioi gmail.com> writes:
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
parent reply "H. S. Teoh" <hsteoh qfbox.info> writes:
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:
 ```
 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
I.e.: ``` write("something"); stdout.flush; ``` T -- MICROSOFT = Most Intelligent Customers Realize Our Software Only Fools Teenagers
Dec 06
parent Andy Valencia <dont spam.me> writes:
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
prev sibling parent Peter C <peterc gmail.com> writes:
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