www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How stdin.readf ignore space char?

reply lili <akozhao tencent.com> writes:
Hi guys:

    input is two line, first line is a number n and second line is 
n float point number,
     e.g. 3
          1.1 2.2 3.3
     how use stdin.readf get all number. i code below but not work 
well.

         int n;
         stdin.readf!"%d\n"(n);
         writeln("read n:",n);

         foreach (i; 0..n)
         {
                 double x;
                 stdin.readf!"%f"(x); //only return first number, 
why?
                 writeln(x);
         }
Sep 03 2019
next sibling parent reply a11e99z <black80 bk.ru> writes:
On Tuesday, 3 September 2019 at 12:55:29 UTC, lili wrote:
 Hi guys:

    input is two line, first line is a number n and second line 
 is n float point number,
     e.g. 3
          1.1 2.2 3.3
     how use stdin.readf get all number.
https://dlang.org/library/std/stdio/readf.html see example
Sep 03 2019
parent reply lili <akozhao tencent.com> writes:
On Tuesday, 3 September 2019 at 13:21:16 UTC, a11e99z wrote:
 On Tuesday, 3 September 2019 at 12:55:29 UTC, lili wrote:
 Hi guys:

    input is two line, first line is a number n and second line 
 is n float point number,
     e.g. 3
          1.1 2.2 3.3
     how use stdin.readf get all number.
https://dlang.org/library/std/stdio/readf.html see example
string d = "3.4 3.3 "; double a; double b; //formattedRead(d,"%f %f", &a, &b); d.readf!" %s %s"(a,b);//hug writeln(a,b);
Sep 03 2019
parent a11e99z <black80 bk.ru> writes:
On Tuesday, 3 September 2019 at 13:25:30 UTC, lili wrote:
 On Tuesday, 3 September 2019 at 13:21:16 UTC, a11e99z wrote:
 On Tuesday, 3 September 2019 at 12:55:29 UTC, lili wrote:
string d = "3.4 3.3 "; double a; double b; //formattedRead(d,"%f %f", &a, &b); d.readf!" %s %s"(a,b);//hug writeln(a,b);
and this too
 string ds = "1.1 2.2 3.3";
 double[] darr;
 ds.formattedRead!"%( %s%)"( darr );
 darr.writeln;
Sep 03 2019
prev sibling parent a11e99z <black80 bk.ru> writes:
On Tuesday, 3 September 2019 at 12:55:29 UTC, lili wrote:
 Hi guys:

    input is two line, first line is a number n and second line 
 is n float point number,
     e.g. 3
          1.1 2.2 3.3
     how use stdin.readf get all number.
u can read array this way too
 auto darr = readln.split.map!(x => x.to!double).array;
 darr.writeln;
Sep 03 2019