#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    21
    Rep Power
    0

    a C input question


    Hi, guys.....I have a question about keyboard input in C

    I m writing a program...
    which allows user to type command in the the prompt....then my program will do specified thing and so on

    command are in the form: com parameter1 parameter2(or more)

    for ex.
    > move queue queue2

    or
    > add 12345 aibo queue2

    the problem is, I don't know how to get those input...
    because scanf can't get the whole string, it's space delimited....
    also, since each of my command can have different # of parameters, this makes it more difficult, otherwise, I would have delcare a char* variable for each....

    my question is is there any way to get the whole command in one string? then parse it?

    or there's a better way to do it??

    tks guys....
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    68
    Rep Power
    12
    Are you talking about command line parameters to your program, or interactive commands once the program is started? If it's the former you can do it like this:
    Code:
    int main(int argc, char *argv[]) {
      // argc contains the number of parameters including the program name
      // argv contains the actual strings for each parameter
    }
    For the latter, fgets would be your best bet to get a whole line for parsing:
    Code:
    if (fgets(buff, BUFSIZE, stdin) != NULL) {
      // Parse buff
    }
  4. #3
  5. It Doesn't Work!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Slovenia
    Posts
    112
    Rep Power
    12
    gets();
    or scanf("%s[^\n]", &bla);
    or something like that
    Last edited by Loser; September 16th, 2003 at 10:20 AM.

IMN logo majestic logo threadwatch logo seochat tools logo