basic interactive readf from stdin

Jay Norwood via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Dec 26 16:13:57 PST 2015


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.

The use of readf into a string is demonstrated in a stdio.d unit 
test.  I assumed it might also work with stdin.

         string s;
         auto f = File(deleteme);
         f.readf("%s\n", &s);
         assert(s == "hello", "["~s~"]");
         f.readf("%s\n", &s);
         assert(s == "world", "["~s~"]");

=============
I did get this below to work with readln, although since readln 
didn't consume the terminator, I had to add the chomp() call.

import std.stdio;
import std.string;

int main(string[] argv)
{
   string nm, nm2;
   nm=readln('\n');
   nm2 = nm.chomp();
   writeln("nm:",nm2);

   nm=readln('\n');
   nm2 = nm.chomp();
   writeln("nm:",nm2);

   return 0;
}


More information about the Digitalmars-d-learn mailing list