View previous topic :: View next topic |
Author |
Message |
genisis329
Joined: 29 Mar 2007 Posts: 1
|
Posted: Thu Mar 29, 2007 12:11 pm Post subject: scanf simple question |
|
|
I have tried to just get a line of data from the user using scanf and it gives me and Access Violation error. Is there some way to make this work?
Code: | import std.stdio;
void main(char[][] args)
{
char[] input;
scanf("%s",input);
writef(input);
} |
|
|
Back to top |
|
|
jcc7
Joined: 22 Feb 2004 Posts: 657 Location: Muskogee, OK, USA
|
Posted: Thu Mar 29, 2007 2:05 pm Post subject: Re: scanf simple question |
|
|
I've had troube getting scanf to read a string, but I'd bet that the brand new std.stdio.readln will do what you want. (You'll need to use DMD 1.010.)
Here's an example straight from the Spec: Code: | import std.stdio;
int main()
{
char[] buf;
while (readln(stdin, buf))
writef("%s", buf);
return 0;
} |
|
|
Back to top |
|
|
Bradley Smith
Joined: 20 Jun 2006 Posts: 60
|
Posted: Thu Mar 29, 2007 4:30 pm Post subject: |
|
|
The C library scanf is used because std.stdio contains the following:
Code: | public import std.c.stdio;
|
Therefore, a char* must be passed to scanf. For example,
Code: | import std.stdio;
import std.c.string;
void main(char[][] args)
{
char[1024] buffer;
scanf("%s", buffer.ptr);
char[] input = buffer[0 .. strlen(buffer.ptr)];
writef("%s", input);
}
|
|
|
Back to top |
|
|
|