www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Question regarding readf

reply Andre Pany <andre s-e-a-p.de> writes:
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é
Apr 22 2019
next sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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
Apr 22 2019
parent reply Andre Pany <andre s-e-a-p.de> writes:
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 formattedRead. Does that make sense to you? Kind regards Andre
Apr 22 2019
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 04/22/2019 03:29 PM, Andre Pany wrote:

 I wonder wheter std.stdio is missing a readfln function. Which executes

 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
Apr 22 2019
parent Andre Pany <andre s-e-a-p.de> writes:
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

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é
Apr 23 2019
prev sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
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.)
Apr 22 2019
parent diniz <diniz posteo.net> writes:
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}
Apr 23 2019