Thread overview
Question regarding readf
Apr 22, 2019
Andre Pany
Apr 22, 2019
Ali Çehreli
Apr 22, 2019
Andre Pany
Apr 22, 2019
Ali Çehreli
Apr 23, 2019
Andre Pany
Apr 22, 2019
Adam D. Ruppe
Apr 23, 2019
diniz
April 22, 2019
Hi,

following the example from http://ddili.org/ders/d.en/input.html,
I try to read a value from console on windows (powershell and dos console).

The code from the example does not work,
writeln is never executed.

import std;

void main()
{
	while(true)
	{
		string name;
		readf("%s", name);
		writeln(name);
        }
}

also this doesn't work:
readf(" %s", name);

It only works while changing the line to:
readf("%s\n", name);

I wonder why the example from Ali is invalid. Did the behavior changed in the past?

Kind regards
André
April 22, 2019
On 04/22/2019 01:45 PM, Andre Pany wrote:
> Hi,
> 
> following the example from http://ddili.org/ders/d.en/input.html,
> I try to read a value from console on windows (powershell and dos console).
> 
> The code from the example does not work,
> writeln is never executed.
> 
> import std;
> 
> void main()
> {
>      while(true)
>      {
>          string name;
>          readf("%s", name);
>          writeln(name);
>          }
> }
> 
> also this doesn't work:
> readf(" %s", name);

The solution is to use readln, which regrettably comes too late in the book:

  http://ddili.org/ders/d.en/strings.html

Ali
April 22, 2019
On Monday, 22 April 2019 at 20:45:54 UTC, Andre Pany wrote:
> The code from the example does not work,
> writeln is never executed.

For "%s" with a string argument, it reads ALL of stdin into that string. This means you need to send an end-of-file indicator to the program. ctrl+z on Windows does this, and ctrl+d can on Linux (you might have to hit it twice there; it doesn't technically send end of file, but can be read as it by the program if there is no other input pending in the buffer).

This is quite bizarre for new users, I agree, but it isn't technically invalid.

(my personal feeling though is readf is just a pile of confusion and should almost never be used. I hate that it is introduced so early in most tutorials... I'd rather have it in an appendix for special cases only rather than like page 3.)
April 22, 2019
On Monday, 22 April 2019 at 22:08:33 UTC, Ali Çehreli wrote:
> On 04/22/2019 01:45 PM, Andre Pany wrote:
>> Hi,
>> 
>> following the example from http://ddili.org/ders/d.en/input.html,
>> I try to read a value from console on windows (powershell and dos console).
>> 
>> The code from the example does not work,
>> writeln is never executed.
>> 
>> import std;
>> 
>> void main()
>> {
>>      while(true)
>>      {
>>          string name;
>>          readf("%s", name);
>>          writeln(name);
>>          }
>> }
>> 
>> also this doesn't work:
>> readf(" %s", name);
>
> The solution is to use readln, which regrettably comes too late in the book:
>
>   http://ddili.org/ders/d.en/strings.html
> Ali

I try to rewrite an example from another language. 4 values are read from console. While readln with strip (and in 2 cases to!int) works fine, it just not looks that good in comparison to the other language.

I wonder wheter std.stdio is missing a readfln function. Which executes readln, strips the #10#13 characters and then executes formattedRead.
Does that make sense to you?

Kind regards
Andre
April 22, 2019
On 04/22/2019 03:29 PM, Andre Pany wrote:

> I wonder wheter std.stdio is missing a readfln function. Which executes
> readln, strips the #10#13 characters and then executes formattedRead.
> Does that make sense to you?

Makes sense but what we are missing is standard streaming (like C++'s overloaded << and >> operators), which Steven Schveighoffer's iopipe can be a solution for but I haven't played with it myself.

Ali

April 23, 2019
Le 23/04/2019 à 00:23, Adam D. Ruppe via Digitalmars-d-learn a écrit :
> (my personal feeling though is readf is just a pile of confusion and should almost never be used. I hate that it is introduced so early in most tutorials... I'd rather have it in an appendix for special cases only rather than like page 3.)

agreed

-- 
diniz {la vita e estranj}
April 23, 2019
On Monday, 22 April 2019 at 23:53:59 UTC, Ali Çehreli wrote:
> On 04/22/2019 03:29 PM, Andre Pany wrote:
>
> > I wonder wheter std.stdio is missing a readfln function.
> Which executes
> > readln, strips the #10#13 characters and then executes
> formattedRead.
> > Does that make sense to you?
>
> Makes sense but what we are missing is standard streaming (like C++'s overloaded << and >> operators), which Steven Schveighoffer's iopipe can be a solution for but I haven't played with it myself.
>
> Ali

Issue created:
https://issues.dlang.org/show_bug.cgi?id=19820

Kind regards
André