CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Oct 2003
    Posts
    58

    understanding commandline

    Hello.

    I have programmed MFC for more than 5 years and now it is a little bit funny for me, to program for commandline and understanding ist. So dont be angry because of stupid questions.

    If I have this command line:

    MyProg -Channels 14 -port 8000 -service console

    And the program ist named MyProg

    What can I expect exactly as argc and char* argv[] at the entrypoint main:
    int main(int argc, char* argv[])
    {
    }

    And can sombody explain, why I can expect these things?

    Thanks for help.

    M.D.

  2. #2
    Join Date
    Jun 2003
    Location
    Gjøvik, Norway
    Posts
    204
    Couldn't you just try, and print'em out?

    Anyhoo:
    // argc = the count of space separated "words" in the commandline:
    argc = 7

    // Each "word" has its own index in the argv array:
    argv[0] = "MyProg"
    argv[1] = "-Channels"
    argv[2] = "14"
    argv[3] = "-port"
    argv[4] = "8000"
    argv[5] = "-service"
    argv[6] = "console"

  3. #3
    Join Date
    Dec 2001
    Location
    Ontario, Canada
    Posts
    2,236
    argv[0] is usually the name of the program.

  4. #4
    Join Date
    Mar 2004
    Location
    Israel
    Posts
    638
    mwilliamson posted:

    argv[0] is usually the name of the program
    what could it be else than the name of the program?
    **** **** **** **** **/**

  5. #5
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401
    Originally posted by Guysl
    what could it be else than the name of the program?
    Well, if you want to be exact you should say it is the name by which the program is called.
    Example:
    Code:
    [treuss]src>cat foo.cc
    #include <iostream>
    
    int main( int argc, char* argv[] )
    {
            std::cout << "argv[0] is \"" << argv[0] << "\"" << std::endl;
    }
    [treuss]src>g++ -o foo foo.cc
    [treuss]src>ln -s foo bar
    [treuss]src>./bar
    argv[0] is "./bar"
    The program binary has the name "foo", but I call it via a symbolic link under the name bar.

  6. #6
    Join Date
    Mar 2004
    Location
    Israel
    Posts
    638
    treuss,

    well, isn't a symbolic link supposed to be an alias name for
    the program, which means it is a name of the program
    (among other "names")?
    **** **** **** **** **/**

  7. #7
    Join Date
    Oct 2003
    Posts
    58
    Thank you for help.

    I have never though it would be such an horror to program without MFC-Classes. I am too familliar with these classes. So the beginning is al little bit hard for me. So I dont see the wood for the threes. Sorry, for this banal question.

    Your answers helped me a lot.

    Thanks.

    M.D.

  8. #8
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401
    Originally posted by Guysl
    well, isn't a symbolic link supposed to be an alias name for the program
    Yepp. Its an alias. So it is not the name (if there is such a thing). Point is that if you deliver software you usually expect that the binary is not renamed by the user who installs it. But you cannot rely on that argv[0] will hold exactly that name, because argv[0] could be "foo", "/usr/bin/foo", "C:\foo" or "bar".
    This is of course rather academic, as I don't know why anybody would rely on argv[0] having a certain value...

  9. #9
    Join Date
    Mar 2004
    Location
    Israel
    Posts
    638
    Good point, treuss.
    **** **** **** **** **/**

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured