www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - readf anyone?

reply Tyro[a.c.edwards] <no.spam home.com> writes:
So I'm digging in and enjoying the read (and let me tell you it's a
pretty interesting read), then I flip to page 22 (I read slow) and
stumbled onto this code:

for (double x; readf(" % ", &x) == 1; ) {
   [snip]
}

Wait that don't look right. I don't remember readf() being
implemented. So I decided to check things out. I jimmied up this
little rig:

void main() {
	double q;
	readf("%s", &q);
	writeln(q);
}

and launched it:

D:\code>dmd input
input.d(5): Error: undefined identifier readf, did you mean function
readln?

well that didn't work. Maybe I'm missing something. Let me take a
look an the phobos doc: no, no mention there. Oh, I got it, I don't
have the latest compiler (how stupid of me). Downloading...
Installing... recompiling. Damn, that wasn't it. Ok, let me take a
look at std.stdio.d. There it is, it's a member function of class
File. Ok, I should be able to access it through stdin. Let me try
that instead:

void main() {
	double q;
	stdin.readf("%s", &q);
	writeln(q);
}

Here goes nothing:

D:\code>dmd read
d:\dmd2\windows\bin\..\..\src\phobos\std\format.d(2948): Error: no
property 'len
gth' for type 'InputByChar'

Damn, that didn't work either. Now that's crazy, why would we use
this function in the book and not take the extra effort to ensure
that it's actually implemented before going to print? Was this an
oversight? If so, can we please get the fix.
Jun 22 2010
next sibling parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 06/22/2010 07:20 AM, Tyro[a.c.edwards] wrote:
 So I'm digging in and enjoying the read (and let me tell you it's a
 pretty interesting read), then I flip to page 22 (I read slow) and
 stumbled onto this code:

 for (double x; readf(" % ",&x) == 1; ) {
     [snip]
 }

 Wait that don't look right. I don't remember readf() being
 implemented. So I decided to check things out. I jimmied up this
 little rig:

 void main() {
 	double q;
 	readf("%s",&q);
 	writeln(q);
 }

 and launched it:

 D:\code>dmd input
 input.d(5): Error: undefined identifier readf, did you mean function
 readln?

 well that didn't work. Maybe I'm missing something. Let me take a
 look an the phobos doc: no, no mention there. Oh, I got it, I don't
 have the latest compiler (how stupid of me). Downloading...
 Installing... recompiling. Damn, that wasn't it. Ok, let me take a
 look at std.stdio.d. There it is, it's a member function of class
 File. Ok, I should be able to access it through stdin. Let me try
 that instead:

 void main() {
 	double q;
 	stdin.readf("%s",&q);
 	writeln(q);
 }

 Here goes nothing:

 D:\code>dmd read
 d:\dmd2\windows\bin\..\..\src\phobos\std\format.d(2948): Error: no
 property 'len
 gth' for type 'InputByChar'

 Damn, that didn't work either. Now that's crazy, why would we use
 this function in the book and not take the extra effort to ensure
 that it's actually implemented before going to print? Was this an
 oversight? If so, can we please get the fix.

Sorry! I've had the message "TODO: implement readf" for a good while emitted during Phobos builds. You read too fast :o). I'll implement it today. I actually have the code somewhere already. Apologies. Andrei
Jun 22 2010
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
Andrei Alexandrescu wrote:

 Sorry! I've had the message "TODO: implement readf" for a good while 
 emitted during Phobos builds. You read too fast :o). I'll implement it 
 today. I actually have the code somewhere already. Apologies.

Will that work with any type like format does? There are unformat and formattedRead in format.d. Is FormatInfo a user serviceable part? :) Could you please show an example on how to unformat any type. It may already be in the book but I haven't gotten to that point yet. :) Ali
Jun 22 2010
prev sibling parent reply =?iso-8859-2?B?VG9tZWsgU293afFza2k=?= <just ask.me> writes:
 	double q;
 	readf("%s", &q);

Why it takes a pointer, not a ref?
Jun 22 2010
parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Tue, 22 Jun 2010 16:42:46 -0400, Tomek SowiƄski <just ask.me> wrote:

 	double q;
 	readf("%s", &q);

Why it takes a pointer, not a ref?

Probably because it's variadic, so the compiler will pass by value if you don't use a pointer. -Steve
Jun 22 2010