FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

scanf simple question

 
Post new topic   Reply to topic     Forum Index -> Tutorials
View previous topic :: View next topic  
Author Message
genisis329



Joined: 29 Mar 2007
Posts: 1

PostPosted: Thu Mar 29, 2007 12:11 pm    Post subject: scanf simple question Reply with quote

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
View user's profile Send private message
jcc7



Joined: 22 Feb 2004
Posts: 657
Location: Muskogee, OK, USA

PostPosted: Thu Mar 29, 2007 2:05 pm    Post subject: Re: scanf simple question Reply with quote

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
View user's profile Send private message AIM Address
Bradley Smith



Joined: 20 Jun 2006
Posts: 60

PostPosted: Thu Mar 29, 2007 4:30 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> Tutorials All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group