basic interactive readf from stdin

Jay Norwood via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Dec 26 15:31:41 PST 2015


On Saturday, 26 December 2015 at 20:38:52 UTC, tcak wrote:
> On Saturday, 26 December 2015 at 20:19:08 UTC, Adam D. Ruppe 
> wrote:
>> On Saturday, 26 December 2015 at 20:11:27 UTC, karthikeyan 
>> wrote:
>>> I experience the same as the OP on Linux Mint 15 with 
>>> dmd2.069 and 64 bit machine.  I have to press enter twice to 
>>> get the output. I read http://ddili.org/ders/d.en/input.html 
>>> and inserted a space before %s but still no use. Am I missing 
>>> something here with the latest version?
>>
>> Oh, I'm sorry, it isn't buffering, it is readfing into a 
>> string here which is weird. Maybe try readln instead of readf.
>
> As far as I remember, in C, if I was to be putting "\n" in 
> scanf after %s, that double entering was happening. I guess 
> that's the same problem. Trying same code without \n in readf 
> can fix it I guess.

import std.stdio;

int main(string[] argv)
{
  string nm;
  stdin.readf("%s",&nm);
  writeln("nm:",nm);
  stdout.flush();
  stdin.readf("%s",&nm);
  writeln("nm:",nm);
  stdout.flush();

  return 0;
}

ok, I tried above, adding both the stdout.flush() and removing 
the \n from the format. It didn't write to output even after a 
couple of enter's.  When I  entered ctrl-Z,  it output below.

============ output running from command prompt
123
456

^Z
nm:123
456


nm:123
456




More information about the Digitalmars-d-learn mailing list