Results 1 to 8 of 8

Thread: c++: command prompt execution with vars

  1. #1
    Join Date
    Jan 2007
    Posts
    629
    Thanks
    10
    Thanked 28 Times in 28 Posts

    Default c++: command prompt execution with vars

    So, suppose I have a program called "hello.exe" and I am using it from the command prompt. How do I pass in vars with the call to the program? (I really have no idea how to phrase it better, sorry .) For example:
    Code:
    hello.exe -g Jas
    So the "-g" and "Jas" are passed into the program. Does that make even the slightest bit of sense?
    --Jas
    function GreatMinds(){ return "Think Like Jas"; }
    I'm gone for a while, but in the meantime: Try using my FTP script | Fight Bot Form Submissions

  2. #2
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    Depends what language you've developed the program in.
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

  3. #3
    Join Date
    Jun 2006
    Posts
    182
    Thanks
    0
    Thanked 14 Times in 14 Posts

    Default

    You use the two parameters passed to the main function for that.
    Code:
    int main(int argc, char* argv[])
    {
        // argc is the total number of arguments (including the executable name)
        // use argv to get the arguments
        // argv[0] - the executable name ("hello.exe")
        // argv[1] - "-j"
        // argv[2] - "Jas"
    }

  4. The Following User Says Thank You to DimX For This Useful Post:

    Jas (06-16-2008)

  5. #4
    Join Date
    Jan 2007
    Posts
    629
    Thanks
    10
    Thanked 28 Times in 28 Posts

    Default

    C++ isn't as hard as I thought it would be to learn

    Quote Originally Posted by tech_support View Post
    Depends what language you've developed the program in.
    Sorry. Its C++ (It's in the title, but I forgot it in the post.)

    DimX: Thanks, that worked great after I played around with it. It's so obvious, and yet I never would have figured it out.

    Is there a way to make it so that the teminal/command prompt will use characters like the pipe (|), amp (&), etc as regular characters in the call to my program?

    For example:
    Code:
    hello.exe -g Jas "This|is|a|string"|with|"pipe|characters"|&|"amps"|and|quotes
    Last edited by Jas; 06-16-2008 at 04:21 AM.
    --Jas
    function GreatMinds(){ return "Think Like Jas"; }
    I'm gone for a while, but in the meantime: Try using my FTP script | Fight Bot Form Submissions

  6. #5
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default

    As for the quotes, yes. Ampersand, yes. Pipe, so to speak, I don't know.

    -magicyte

  7. #6
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default

    By the way, I forgot something to tell you. When taking in quotes("), the MS-DOS thing, WHATEVER, the stuff in between the two quotes counts as one whole character array. It order to take in multiple characters, you would need to create more variables in the main() function.

    -magicyte

  8. #7
    Join Date
    Mar 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by DimX View Post
    You use the two parameters passed to the main function for that.
    Code:
    int main(int argc, char* argv[])
    {
        // argc is the total number of arguments (including the executable name)
        // use argv to get the arguments
        // argv[0] - the executable name ("hello.exe")
        // argv[1] - "-j"
        // argv[2] - "Jas"
    }
    What do you mean up above next to the //. Do I have to include anything aswell, please show an example. Im just a noob, Im using Visual C++ 2008 Express Edition.

  9. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Two arguments are passed to main() by the operating system: an integer number of arguments passed, and a char*[] (array of strings [arrays of characters]) containing the arguments.

    On POSIX systems there is a function getopt() to parse these options, but on Windows there's nothing.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •