How does buffering actually work?

Cleverson Casarin Uliana clul at mm.st
Thu Feb 28 21:17:23 UTC 2019


Hi all, this may be a really newbie question but I'm not really used to 
medium/low level stuff like this. I've actually done the same in Python, 
and it just works...

Supose I want my program to print some content on a given line, lets say 
"First name: value1", then it should sit down and wait for me to press 
Enter, then print "Second name: value2", then sit down again, and so on. 
I've tried to do that the following way:

import std.stdio;
void myFunc(string name, double value) {
write(name, value);
string buf;
readf(" %s", buf);
}

then I just call multiple times:

void main() {
myFunc("First name: ", value1);
myFunc("Second name: ", value2);
(...)
}

It works almost perfectly, except that it doesn't wait for my first 
Enter after printing "First name: value1". Rather, it prints both "First 
name: value1" and "First name: value2" together on the same line, then 
it starts to behave as expected, e.g. printing one line at a time and 
waiting for me to press Enter.

I experimented substituting readf for readln, but then it doesn't 
recognize my Enter presses and hangs on.

What is a more suitable aproach to this problem please?

Thanks,
Cleverson


More information about the Digitalmars-d-learn mailing list