Thread overview
How does buffering actually work?
Feb 28, 2019
Ali Çehreli
Feb 28, 2019
sarn
Feb 28, 2019
Adam D. Ruppe
February 28, 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
February 28, 2019
On 02/28/2019 01:17 PM, Cleverson Casarin Uliana wrote:

> 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?

readln and strip works, and formattedRead can be useful as well. I have an example here:

  http://ddili.org/ders/d.en/strings.html#ix_strings.readln

import std.stdio;
import std.string;

void main() {
    char[] name;

    write("What is your name? ");
    readln(name);
    name = strip(name);

    writeln("Hello ", name, "!");
}

Ali

February 28, 2019
On Thursday, 28 February 2019 at 21:17:23 UTC, Cleverson Casarin Uliana wrote:
> 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.

Perhaps that happened with some other variation of the code.  The code you wrote shouldn't work like that (it doesn't for me when I tried, at least).

Ali has some good answers for fixing your code.  (readf("\n") also works, BTW.)  Hopefully this helps with the "How does buffering actually work?" question:

D uses the system's standard C library for IO, like most programming languages do, so IO buffering isn't fundamentally different (but some high-level functions might have different behaviour).

The standard C library provides buffered IO for input and output.  By default terminal IO is line-buffered (not sure if that's all systems), so you might see delays up until a newline, but line-by-line IO won't notice the buffering.

What happens here?

write()
read()
write()
read()

The first write goes to the output buffer.  If the buffer ever gets full (or has a newline in the case of line buffering), the data gets flushed to the real output.

At the read, it's possible there's still some data in the output buffer that's not flushed.  If needed, you can explicitly call flush() to make sure there isn't.  If there happens to already be data in the read buffer, read() will take as much as it needs to.  If there isn't enough, then real input will happen, and the call will block until data comes in.  The real read will ask for a chunk of data, which will often be more than the read() call needs.  The remainder gets put into the buffer (that's what it's for).  (The kernel and libc actually both have IO buffers.)

In any case, the second write won't happen until the read has finished.

Rinse and repeat for the remaining lines.
February 28, 2019
Hi, thank you both Ali and Sarn.

How am I suposed to use the flush function? I've found it in the std.stdio reference, buth calling it in my code gives "undefined identifier", even though std.stdio is imported.

Greetings,
Cleverson
February 28, 2019
On Thursday, 28 February 2019 at 23:07:44 UTC, Cleverson Casarin Uliana wrote:
> How am I suposed to use the flush function? I've found it in the std.stdio reference, buth calling it in my code gives "undefined identifier", even though std.stdio is imported.


try

stdout.flush();


flush is a member function of the file object.
February 28, 2019
Thanks Adam, I'll test it later.

Cheers
Cleverson