Thread: Creating command line

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    6

    Creating command line

    Hi

    I'm making an application with a commandline. Not, starting the application with arguments, but a commandline within it.

    So, i reseived data with scanf() and i've to check it.

    My question: how do i put the command in an array as argv[] ?
    So, that the user sends:
    logon phpnerd 8549256

    and the fucntions puts that in an array:
    argv[0] = "logon";
    argv[1] = "phpnerd";
    argv[2] = "8549256";

    Thnx

    PHPnerd

    (my nick isn't really true )

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Why strcpy is not good?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    6
    copying is not the problem, but exploding it by the ' ' (space) is the problem

    PHPnerd

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    You want to parse the string stopping on whitespaces?
    scanf("%s %s %s", buf1, buf2, buf3);

    //yeah, yeah, I know %s is not safe - but just to show the idea

    %20s is better for buffer long enough

    The second approach - use strchr + strncpy for example
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    6
    aah, ok, then i have to make a scanf() in stead of gets() :P

    (ive to create all functions by myself)

    PHP

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Never use gets.
    Use fgets instead of gets.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    6
    lolz, i can't use scanf() and fgets() because i dont have they

    Who has a scanf() function for me?

    PHPnerd

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Um, what can you use?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Feb 2008
    Posts
    6
    Wait a moment...

    gets()
    printf()
    vsprintf_help();
    sprintf();
    printf();
    vprintf();
    vsprintf();
    strcmp ();
    strpbrk ();
    strspn();
    strchr ();
    strcpy();
    puts();
    putch();
    memcpy();
    memset();
    memsetw();
    strlen();

    and any other functions, but not for this needed (drivers)

    I can add some new functions if needed

    PHPnerd

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    so use strchr to find a space
    and memcpy to copy a regeon from one space to another
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  11. #11
    Registered User
    Join Date
    Feb 2008
    Posts
    6
    do i need sizeof() for argument 3 of memcpy() ?

    PHP

  12. #12
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by PHPnerd View Post
    do i need sizeof() for argument 3 of memcpy() ?

    PHP
    No, sizeof is of no help here...

    Show your code for father questions
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. Creating a rain effect...
    By Raigne in forum Game Programming
    Replies: 11
    Last Post: 12-04-2007, 05:30 PM
  3. Creating Custom Controls
    By Grayson_Peddie in forum C# Programming
    Replies: 1
    Last Post: 12-29-2003, 01:41 PM
  4. problems creating a linked list
    By jamjar in forum C Programming
    Replies: 5
    Last Post: 10-23-2002, 05:50 AM
  5. Creating an Code-template in Visual C++
    By yerrel in forum C++ Programming
    Replies: 1
    Last Post: 06-10-2002, 11:25 AM