www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - readf?

reply Fredrik Olsson <peylow gmail.com> writes:
At http://www.algonet.se/~afb/d/stdio/ in stdio.d, Anders F Björklund 
have a a stdio with readf functions. Is there a reason that these are 
not present in the official Phobos?

And what is the preferred way to read stuff from the keyboard? 
Writef/writefln is a superb mix of the powers of Pascal's write and 
writeln combined with C's printf. There should be an equally powerful 
way to read data, should there not?

regards
	Fredrik Olsson
Oct 12 2005
next sibling parent reply "Ben Hinkle" <ben.hinkle gmail.com> writes:
"Fredrik Olsson" <peylow gmail.com> wrote in message 
news:dij444$1c93$1 digitaldaemon.com...
 At http://www.algonet.se/~afb/d/stdio/ in stdio.d, Anders F Björklund have 
 a a stdio with readf functions. Is there a reason that these are not 
 present in the official Phobos?

 And what is the preferred way to read stuff from the keyboard? 
 Writef/writefln is a superb mix of the powers of Pascal's write and 
 writeln combined with C's printf. There should be an equally powerful way 
 to read data, should there not?

 regards
 Fredrik Olsson

check out http://www.digitalmars.com/d/phobos/std_stream.html. The stream wrapper for stdin is std.cstream.din.
Oct 12 2005
parent reply Ivan Senji <ivan.senji_REMOVE_ _THIS__gmail.com> writes:
Ben Hinkle wrote:
 check out http://www.digitalmars.com/d/phobos/std_stream.html. The stream 
 wrapper for stdin is std.cstream.din. 
 

I was wondering today why din is in std.cstream and not int std.stream where it would IMO be better to be? If i wan't to use streams and din why the need to import both stream and cstream?
Oct 12 2005
parent reply Deewiant <deewiant.doesnotlike.spam gmail.com> writes:
Ivan Senji wrote:
 Ben Hinkle wrote:
 
 check out http://www.digitalmars.com/d/phobos/std_stream.html. The
 stream wrapper for stdin is std.cstream.din.

I was wondering today why din is in std.cstream and not int std.stream where it would IMO be better to be? If i wan't to use streams and din why the need to import both stream and cstream?

There is no need, std.cstream publically imports std.stream.
Oct 12 2005
parent reply Ivan Senji <ivan.senji_REMOVE_ _THIS__gmail.com> writes:
Deewiant wrote:
 Ivan Senji wrote:
 
Ben Hinkle wrote:


check out http://www.digitalmars.com/d/phobos/std_stream.html. The
stream wrapper for stdin is std.cstream.din.

I was wondering today why din is in std.cstream and not int std.stream where it would IMO be better to be? If i wan't to use streams and din why the need to import both stream and cstream?

There is no need, std.cstream publically imports std.stream.

Maybe this doesn't make sense, but i don't need cstream's CFile only din/dout, so i would like not to have to import cstream. Now that i think about it is there any reason why CFile is not in std.stream? If there is i can't see it.
Oct 12 2005
parent reply "Ben Hinkle" <ben.hinkle gmail.com> writes:
"Ivan Senji" <ivan.senji_REMOVE_ _THIS__gmail.com> wrote in message 
news:dijt1a$2bqk$1 digitaldaemon.com...
 Deewiant wrote:
 Ivan Senji wrote:

Ben Hinkle wrote:


check out http://www.digitalmars.com/d/phobos/std_stream.html. The
stream wrapper for stdin is std.cstream.din.

I was wondering today why din is in std.cstream and not int std.stream where it would IMO be better to be? If i wan't to use streams and din why the need to import both stream and cstream?

There is no need, std.cstream publically imports std.stream.

Maybe this doesn't make sense, but i don't need cstream's CFile only din/dout, so i would like not to have to import cstream. Now that i think about it is there any reason why CFile is not in std.stream? If there is i can't see it.

std.cstream also publically imports std.c.stdio. The reason for making cstream was because 1) std.stream is already big and 2) cstream is for the C library wrappers and users should be aware of when they are wrapping the C stdin/out etc instead of using the native OS interface to console io.
Oct 12 2005
parent reply Ivan Senji <ivan.senji_REMOVE_ _THIS__gmail.com> writes:
Ben Hinkle wrote:
 "Ivan Senji" <ivan.senji_REMOVE_ _THIS__gmail.com> wrote in message 
 news:dijt1a$2bqk$1 digitaldaemon.com...

Maybe this doesn't make sense, but i don't need cstream's CFile only
din/dout, so i would like not to have to import cstream.

Now that i think about it is there any reason why CFile is not in 
std.stream? If there is i can't see it.

std.cstream also publically imports std.c.stdio. The reason for making cstream was because 1) std.stream is already big and 2) cstream is for the C library wrappers and users should be aware of when they are wrapping the C stdin/out etc instead of using the native OS interface to console io.

Most of what you say makes sense and i understand it but not the last part about wraping stdin/out vs. OS interface to console io? My class has toStream(Stream s) method and sometimes i would like it to output to console and sometimes to memory or file stream, sometimes i need obj.toStream(dout) or obj.toStream(memoryStream) ... And i don't like importing std.cstream because i do not need the std.c.stdio stuff it publically imports but only din and dout Streams. Aren't din/dout same as c++ cin/cout? I am trying to use them the same way. And i don't think stream's size (~80kB) would incresase that much if cstream (~4kB) was included :)
Oct 12 2005
parent "Ben Hinkle" <bhinkle mathworks.com> writes:
"Ivan Senji" <ivan.senji_REMOVE_ _THIS__gmail.com> wrote in message 
news:dikvs5$1o02$1 digitaldaemon.com...
 Ben Hinkle wrote:
 "Ivan Senji" <ivan.senji_REMOVE_ _THIS__gmail.com> wrote in message 
 news:dijt1a$2bqk$1 digitaldaemon.com...

Maybe this doesn't make sense, but i don't need cstream's CFile only
din/dout, so i would like not to have to import cstream.

Now that i think about it is there any reason why CFile is not in 
std.stream? If there is i can't see it.

std.cstream also publically imports std.c.stdio. The reason for making cstream was because 1) std.stream is already big and 2) cstream is for the C library wrappers and users should be aware of when they are wrapping the C stdin/out etc instead of using the native OS interface to console io.

Most of what you say makes sense and i understand it but not the last part about wraping stdin/out vs. OS interface to console io?

The OS equivalents to stdin are GetStdHandle(STD_INPUT_HANDLE) on Windows and .. I forget... something like STDIN_FILENO on Linux.
 My class has toStream(Stream s) method and sometimes i would like it to 
 output to console and sometimes to memory or file stream, sometimes i need 
 obj.toStream(dout) or obj.toStream(memoryStream) ...
 And i don't like importing std.cstream because i do not need the 
 std.c.stdio stuff it publically imports but only din and dout Streams.

sure. din/dout could be declared in std.stream as simple Stream objects and initialized in std.cstream as CFiles.
 Aren't din/dout same as c++ cin/cout? I am trying to use them the same 
 way.

 And i don't think stream's size (~80kB) would incresase that much if 
 cstream (~4kB) was included :)
 

Oct 13 2005
prev sibling next sibling parent reply Sean Kelly <sean f4.ca> writes:
In article <dij444$1c93$1 digitaldaemon.com>, Fredrik Olsson says...
At http://www.algonet.se/~afb/d/stdio/ in stdio.d, Anders F Björklund 
have a a stdio with readf functions. Is there a reason that these are 
not present in the official Phobos?

When I originally wrote readf I held off on submitting it because TypeInfo for pointers was basically broken. As a result, my code contained a workaround to determine the parameter types, and I didn't feel comfortable submitting a work in progress (readf also relies on changed to std.utf so data can be translated as it is read, so the submission would requiring changing that module as well). Anders later patched my readf to work on GDC (if I remember correctly) and it's been in that state ever since. Personally, I would like to see readf added to Phobos, but as I'm not using Phobos anyway, I haven't pushed for it. Also, as Ben Hinkle has mentioned, the stream classes offer a reasonable alternative for formatted input. I'm fairly certain the readf/scanf functionality isn't as complete as the version I wrote (mine is fully C99 compliant while the std.stream version was an abbreviated implementation way back when I last checked), but it should do for most purposes. And Mango is obviously another option. For what it's worth, my original implementation of readf is available here as "stdio addon": http://www.home.f4.ca/sean/d/ Sean
Oct 12 2005
next sibling parent Sean Kelly <sean f4.ca> writes:
In article <dik142$2jrn$1 digitaldaemon.com>, Sean Kelly says...
I'm fairly certain the readf/scanf functionality isn't as
complete as the version I wrote (mine is fully C99 compliant while the
std.stream version was an abbreviated implementation way back when I last
checked), but it should do for most purposes.

I just checked std.stream and it looks like readf has been fully rewritten. I don't know if it covers all the subtleties of the scanf spec, but at a glance it seems a darn sight better than it used to be. Sean
Oct 12 2005
prev sibling parent reply =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Sean Kelly wrote:

At http://www.algonet.se/~afb/d/stdio/ in stdio.d, Anders F Björklund 
have a a stdio with readf functions. Is there a reason that these are 
not present in the official Phobos?

When I originally wrote readf I held off on submitting it because TypeInfo for pointers was basically broken. As a result, my code contained a workaround to determine the parameter types, and I didn't feel comfortable submitting a work in progress (readf also relies on changed to std.utf so data can be translated as it is read, so the submission would requiring changing that module as well). Anders later patched my readf to work on GDC (if I remember correctly) and it's been in that state ever since.

I'm pretty happy with how the *syntax* turned out, but not the implementation - mostly due to the aforementioned TypeInfo bugs. Since it's using pointers (yes!, by design) it needs typeid for that to return sane results and it didn't so I just hacked it... Passing "mangle" characters around, and who knows what else :-P
 Personally, I would like to see readf added to
 Phobos, but as I'm not using Phobos anyway, I haven't pushed for it.  Also, as
 Ben Hinkle has mentioned, the stream classes offer a reasonable alternative for
 formatted input.  I'm fairly certain the readf/scanf functionality isn't as
 complete as the version I wrote (mine is fully C99 compliant while the
 std.stream version was an abbreviated implementation way back when I last
 checked), but it should do for most purposes.  And Mango is obviously another
 option.

I think most people turned to the various "stream" classes instead... Or just the std.c.stdio, with printf and scanf instead of std.stdio. Might take another look at it, like right after "D 1.0" is released :-) Meanwhile, Sir Writef is fighting all the windmills to find Ms Readf...
 For what it's worth, my original implementation of readf is available here as
 "stdio addon":
 
 http://www.home.f4.ca/sean/d/

Thanks, I should have linked to that in a README or something ? I do believe I did so in the original posting to the newsgroup: http://www.digitalmars.com/d/archives/digitalmars/D/21692.html --anders
Nov 08 2005
parent Sean Kelly <sean f4.ca> writes:
Anders F Björklund wrote:
 
 I'm pretty happy with how the *syntax* turned out, but not the 
 implementation - mostly due to the aforementioned TypeInfo bugs.
 
 Since it's using pointers (yes!, by design) it needs typeid for
 that to return sane results and it didn't so I just hacked it...

Yeah you pretty much have to use pointers, as varargs don't allow you to specify 'out' parameters :) But with the type safety in D, the user would have to work pretty hard to screw something up anyway.
 Might take another look at it, like right after "D 1.0" is released :-)
 Meanwhile, Sir Writef is fighting all the windmills to find Ms Readf...

It would be nice, but I'm devoting most of my nonexistent free time to Ares. I'm not planning to look at readf any time soon either.
 
 For what it's worth, my original implementation of readf is available 
 here as
 "stdio addon":

 http://www.home.f4.ca/sean/d/

Thanks, I should have linked to that in a README or something ? I do believe I did so in the original posting to the newsgroup: http://www.digitalmars.com/d/archives/digitalmars/D/21692.html

I think you did. Sean
Nov 08 2005
prev sibling parent reply =?utf-8?B?RGF3aWQgQ2nEmcW8YXJraWV3aWN6?= <araelx gmail.com> writes:
On Wed, 12 Oct 2005 15:46:43 +0200, Fredrik Olsson <peylow gmail.com>  
wrote:

 At http://www.algonet.se/~afb/d/stdio/ in stdio.d, Anders F Björklund  
 have a a stdio with readf functions. Is there a reason that these are  
 not present in the official Phobos?

 And what is the preferred way to read stuff from the keyboard?  
 Writef/writefln is a superb mix of the powers of Pascal's write and  
 writeln combined with C's printf. There should be an equally powerful  
 way to read data, should there not?

One more question. What are the functions to read int and to know if dumb user didn't entered garbages? I would like an exception, but flag would be sufficient too. -- Dawid Ciężarkiewicz
Oct 18 2005
parent reply =?UTF-8?B?SmFyaS1NYXR0aSBNw6RrZWzDpA==?= <jmjmak invalid_utu.fi> writes:
Dawid Ciężarkiewicz wrote:
 On Wed, 12 Oct 2005 15:46:43 +0200, Fredrik Olsson <peylow gmail.com>  
 wrote:
 
 At http://www.algonet.se/~afb/d/stdio/ in stdio.d, Anders F Björklund  
 have a a stdio with readf functions. Is there a reason that these are  
 not present in the official Phobos?

 And what is the preferred way to read stuff from the keyboard?  
 Writef/writefln is a superb mix of the powers of Pascal's write and  
 writeln combined with C's printf. There should be an equally powerful  
 way to read data, should there not?

One more question. What are the functions to read int and to know if dumb user didn't entered garbages? I would like an exception, but flag would be sufficient too.

I recommend modern programming with exceptions: import std.conv, std.stdio; void main() { char[] s = "123"; int i; try { i = toInt(s); } catch (ConvError e) { writefln("Bad input!"); } catch (ConvOverflowError e) { writefln("Overflow!"); } writefln("Got %d.", i); }
Oct 18 2005
parent =?utf-8?B?RGF3aWQgQ2nEmcW8YXJraWV3aWN6?= <araelx gmail.com> writes:
On Wed, 19 Oct 2005 01:10:51 +0200, Jari-Matti Mäkelä  
<jmjmak invalid_utu.fi> wrote:
  What are the functions to read int and to know if dumb user didn't  
 entered  garbages? I would like an exception, but flag would be  
 sufficient too.

I recommend modern programming with exceptions: import std.conv, std.stdio; void main() { char[] s = "123"; int i; try { i = toInt(s); } catch (ConvError e) { writefln("Bad input!"); } catch (ConvOverflowError e) { writefln("Overflow!"); } writefln("Got %d.", i); }

Emm ... where is any form of "read" "scan" or something actually? I'm looking for standard functions (to read for example int) that throw Exceptions on mistyped input. I know how to use Exceptions. <g> ;-) (You probably misunderstood me. || I haven't make myself clear.) Regards, -- Dawid Ciężarkiewicz
Oct 21 2005