Thread: Parsing cmd line arguments as variables

  1. #1
    Registered User graison's Avatar
    Join Date
    Aug 2011
    Location
    Pluto
    Posts
    2

    Parsing cmd line arguments as variables

    Hi everyone, I was just wondering if there was a standard function in C/C++ where you could convert a string of numbers to separate integer variables. For instance,
    Code:
    char *argv[];
    argv = "1 2";
    int numberone = 1;
    int numbertwo = 2;
    So I basically need a function where I can parse two command line variables as integers.

    Thanks, Graison

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    strtol conveniently tells you where it stopped reading one number, which will easily allow you to start reading a second number.

    EDIT EDIT EDIT: You also need to read that bit about command-line arguments again. You will not get all your command line arguments in one string. You will get each command line argument in its own string.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    First you need to understand how command line arguments work in C...

    Code:
    int main ( int argc, char *argv[] )
    The integer value argc tells you how many command line arguments there are. You need to check this to be sure the command line was correctly entered by the user and to know how many argv[] strings to expect.

    The character arrays at argv[] will contain one string for each command line parameter. The breakpoint in unquoted command lines is a space...

    Thus if the user enters... myprog 1 2 3 ... argc should contain 4 meaning there are 4 parameters. The breakdown is like this...
    argv[0] = myprog
    argv[1] = 1
    argv[2] = 2
    argv[3] = 3

    These are string variables. You can convert the strings to numerical values with atoi() and similar functions.

  4. #4
    Banned
    Join Date
    Aug 2011
    Location
    Ghaziabad, India, India
    Posts
    48
    Tater is correct . . .v/

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Vxyz View Post
    Tater is correct . . .v/
    I've just added you to my ignore list...

    Now be a decent person and Stop clicking the "like" button on all my posts

    I find you to be both stupid and beyond annoying.

  6. #6
    Registered User
    Join Date
    Aug 2010
    Location
    Rochester, NY
    Posts
    196
    That's not very nice :-\.

    The like button exists for a reason - there's a reason stack overflow makes an epic resource - the best answers are thumb-upped - agreed upon by others.

    I think he can just as easily counter with: "Stop answering questions correctly, then" .

  7. #7
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    ....back to the original question:

    @OP: Take a look at FAQ- Access Command Line Arguments
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  8. #8
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by Syndacate View Post
    That's not very nice :-\.

    The like button exists for a reason - there's a reason stack overflow makes an epic resource - the best answers are thumb-upped - agreed upon by others.

    I think he can just as easily counter with: "Stop answering questions correctly, then" .
    Let's stick with the topic at hand. If we keep allowing trolls to get us off topic then this site will degrade into all those other useless ones out there. If you would like to have a conversation, PM Tater. Or perhaps you could even start a thread in General Discussions about the topic you would like to discuss. I did that here a little bit ago. Eitherway, a thread in the C board is not the place to do it.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Syndacate View Post
    That's not very nice :-\.

    The like button exists for a reason - there's a reason stack overflow makes an epic resource - the best answers are thumb-upped - agreed upon by others.

    I think he can just as easily counter with: "Stop answering questions correctly, then" .
    If it's something profound or extremely helpful fine... click the button. But for the last few days more than half of the "likes" I've gotten have all come from this one idiot over perfectly mundane statements. Not only is it a major annoyance to have to clear my notices every couple of minutes, it dilutes the the purpose of the thing into meaninglessness. What's the point of a scoring system if every post gets 100%?
    Last edited by CommonTater; 08-03-2011 at 10:59 AM.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by AndrewHunter View Post
    Let's stick with the topic at hand. If we keep allowing trolls to get us off topic then this site will degrade into all those other useless ones out there. If you would like to have a conversation, PM Tater. Or perhaps you could even start a thread in General Discussions about the topic you would like to discuss. I did that here a little bit ago. Eitherway, a thread in the C board is not the place to do it.
    Agreed... That's why I put him (gleaned from his newly started blog ... and he calls me "dear"!) in my ignore list. Now I don't heve to waste my time on his posts.

    Yes, please... Back to the OP's question...

  11. #11
    Registered User graison's Avatar
    Join Date
    Aug 2011
    Location
    Pluto
    Posts
    2
    Thank you so much CommonTater. Also, I know how you use command-line variables with
    Code:
    int main(int argc, char *argv[])
    but you answered my question perfectly!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. UNIX: Parsing *argv[] with quoted arguments
    By Enfors in forum C Programming
    Replies: 19
    Last Post: 04-03-2007, 06:19 AM
  2. setting variables to command line arguments
    By Calef13 in forum C++ Programming
    Replies: 19
    Last Post: 11-02-2006, 11:44 AM
  3. passing arguments using "Command Line Arguments"
    By Madshan in forum C++ Programming
    Replies: 1
    Last Post: 04-19-2006, 03:46 PM
  4. Parsing a line using sscanf()
    By caduardo21 in forum C Programming
    Replies: 6
    Last Post: 02-10-2005, 07:38 AM
  5. Line Parsing and this loop
    By Joshua-NSW in forum C Programming
    Replies: 2
    Last Post: 11-14-2001, 03:27 AM