Thread overview
readf
Feb 17, 2006
nascent
Feb 17, 2006
Sean Kelly
Feb 17, 2006
Sean Kelly
Feb 18, 2006
nascent
Feb 18, 2006
Sean Kelly
February 17, 2006
Ok, supposedly readf grabs console input from the user. I found this archive http://www.digitalmars.com/d/archives/digitalmars/D/learn/1900.html that wasn't much help. So my questing is how do I use it? Feel free to skip my guesses as to how it works and just tell me.

I will not explain how I think I am supposed to use it. readf("Bla %s ...", var, ...) The first way is "Bla " is printed to the screen and waits for %s input. The second is that it would look at the input string and grab %s.

Example-ish

int integer;
char character;
char[] string;

// Option A
readf("Enter an Int: %d, a character: %s and a string %s",
	&integer, &character, &string);

// Option B
readf("I like %s, my favorite letter is %c, and I have %d meals a day.",
	&string, &character, &integer);


Option A would look something like this: user input is in {}
Enter an Int: {6\n}
a character: {T\n}
and a string {Fly away I tell you.\n}
(integer would be 6, character T, and string Fly away I tell you.)

Option B would look:
{I like cheese, my favorite letter is q, and I have 3 meals a day."\n}
(integer 5, character s, string cheese)

--Jesse
February 17, 2006
nascent wrote:
> Ok, supposedly readf grabs console input from the user. I found this archive http://www.digitalmars.com/d/archives/digitalmars/D/learn/1900.html that wasn't much help. So my questing is how do I use it? Feel free to skip my guesses as to how it works and just tell me.

readf is to scanf what writef is to printf :-)  If no format strings are supplied, default formats will be chosen for the supplied parameters. I'm not sure which version you're using, but here's an example pulled from the unit tests in my original version:

int n, i; double x; char name[50];

n = sreadf( "25 54.32E-1 thompson", "%d%f%s", &i, &x, &name[0] );

Alternately, you can do away with the format string altogether and it will expect the default formats:

int n, i; double x; char name[50];

n = sreadf( "25 54.32E-1 thompson", &i, &x, &name[0] );

For information on the formats accepted, check the C spec, as readf was written to be fully compliant with scanf in C (plus additional support for D-specific types).


Sean
February 17, 2006
By the way, my original readf implementation is available here:

http://home.f4.ca/sean/d/stdio.zip

It requires the included utf module, which is simply a modified version of std.utf to accept delegates as a source/destination for encoding and decoding.


Sean
February 18, 2006
Well, I must not be importing the correct library. I get that sreadf is undefined. Isn't it in stream?

import std.stdio;
import std.stream;
import std.c.stdio;

void main() {
	int n, i; double x; char name[50];

	n = sreadf("25 54.32E-1 thompson", "%d%f%s", &i, &x, &name[0] );

	writef("%d", n);
}


Sean Kelly wrote:
> nascent wrote:
> 
>> Ok, supposedly readf grabs console input from the user. I found this archive http://www.digitalmars.com/d/archives/digitalmars/D/learn/1900.html that wasn't much help. So my questing is how do I use it? Feel free to skip my guesses as to how it works and just tell me.
> 
> 
> readf is to scanf what writef is to printf :-)  If no format strings are supplied, default formats will be chosen for the supplied parameters. I'm not sure which version you're using, but here's an example pulled from the unit tests in my original version:
> 
> int n, i; double x; char name[50];
> 
> n = sreadf( "25 54.32E-1 thompson", "%d%f%s", &i, &x, &name[0] );
> 
> Alternately, you can do away with the format string altogether and it will expect the default formats:
> 
> int n, i; double x; char name[50];
> 
> n = sreadf( "25 54.32E-1 thompson", &i, &x, &name[0] );
> 
> For information on the formats accepted, check the C spec, as readf was written to be fully compliant with scanf in C (plus additional support for D-specific types).
> 
> 
> Sean
February 18, 2006
nascent wrote:
> Well, I must not be importing the correct library. I get that sreadf is undefined. Isn't it in stream?

Oops, I was talking about an extension I wrote that's not in Phobos.  I think std.stream has a similar function as a part of the IStream class.


Sean