Thread: command line parameters

  1. #16
    Registered User
    Join Date
    Jul 2010
    Location
    Oklahoma
    Posts
    107
    CppProgrammer33,

    Tinker with this:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main( int argc, char **argv, char **envp )
    {
    
       auto int i;
       
       for( i = 0; i < argc; i = i + 1 )
       {
          cout << "Argument #" << i << ": " << argv[i] << endl;
       }
    
    // nullptr is undeclared in g++ (GCC) 3.4.4
    //   for( i = 0; nullptr != envp[i]; i = i + 1 )
       for( i = 0; NULL != envp[i]; i = i + 1 )
       {
          cout << "Environment variable #" << i << ": " << envp[i] << endl;
       }
       
       return 0;
    }
    I tried using nullptr, but it looks like it's time to upgrade the software. I think that's the full signature for main, in addition to the parameters passed to the program by the OS from the user when it is invoked, there are a collection of environment variables which may also be accessed by the program.

    Best Regards,

    New Ink -- Henry

  2. #17
    Registered User Cadbury's Avatar
    Join Date
    Aug 2010
    Location
    The Netherlands
    Posts
    3
    Quote Originally Posted by laserlight View Post
    Perhaps you should test with this:
    Code:
    #include <iostream>
    #include <cstring>
    
    int main(int argc, char* argv[])
    {
        using namespace std;
    
        if (argc > 1)
        {
            if (strcmp(argv[1], "-Dev") == 0)
            {
                cout << "Valid command line argument!";
            }
            else
            {
                cout << "Invalid command line argument: '" << argv[1] << "'";
            }
        }
        else
        {
            cout << "No command line argument.";
        }
    
        cout << endl;
    }
    Note that you have to enter -Dev as the command line argument, not say, -dev or -Devmode.exe.
    This is correct.

    @threadstarter

    I'm not sure how you run the file, because the following will work just fine:

    1) Run
    2) cmd.exe
    3) Browse to the folder your program exe is in (cd c:/projects/../)
    4) type in: YourApplicationName.exe -Dev

    Simple

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. Parameters quick Question
    By lifeis2evil in forum C++ Programming
    Replies: 2
    Last Post: 11-18-2007, 11:12 PM
  3. function with variable number of parameters
    By mikahell in forum C++ Programming
    Replies: 3
    Last Post: 07-23-2006, 03:35 PM
  4. Passing parameters from VB to C++ through ActiveX DLL
    By torbjorn in forum Windows Programming
    Replies: 0
    Last Post: 12-10-2002, 03:13 AM
  5. command-line parameters.
    By Tombear in forum C Programming
    Replies: 2
    Last Post: 10-28-2001, 08:40 AM