command line parameters

This is a discussion on command line parameters within the C++ Programming forums, part of the General Programming Boards category; OK, what i want to do is this: recall a long time ago when half life 1 came out the ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    13

    command line parameters

    OK, what i want to do is this: recall a long time ago when half life 1 came out the only way to enable the dev mode was to do this: Appname -Devmode.exe. is that even worth it or should i just not do it cause i tried this:

    Code:
    if(argv[0] == -DevMode)
    {
           cout << "Hello" << endl;
    }
    but that didn't work :-( also i was wondering if theres a way to set the name of the exe
    in the code it self.

    Like:


    [code]

    void setExeName(const char* AppName); and it creates that EXE with that name. i don't mean creating a file with the extention of .exe i mean making the compiler be able to set the name of the exe through code. I hope i'm makling sense

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    Because the command line parameters are all simple C-style strings.

    Also, argv[0] is typically the program name, so all the interesting things start from argv[1]

    Try
    Code:
    if ( strcmp( argv[1], "-devmode") == 0 )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    13
    Hmmm, i get an unhandled exception

    Code:
    "An unhandled win32 exception int Commands-devmode.exe(1040) Just In debugging the exception failed with the following error Just-In-Time debugger not installed.
    I guess Dev C++ doesn't have that cause i can't find it, i should be getting my computer back soon so i'll try in VS C++

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    Did you even supply a command line parameter?

    Not doing so will crash.

    You MUST look at the value of argc before trying to access any argv[ ].
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    13
    so i would do:

    Code:
    for(int index = 0; index < argc; ++index)
    {
         if(argv[index] == 1)
         {
                if(strcmp(argv[index], "-dev") == 0)
                {
                         //Statements
                }
          }
    }
    that's the only way i can think of doing it.........I'm sorry i'm trying to understand this.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,633
    Yes, you could do something like that, but if you are only interested in checking argv[1], then it would be simpler to write:
    Code:
    if (argc > 1)
    {
        if (strcmp(argv[1], "-dev") == 0)
        {
            //Statements
        }
    }
    Last edited by laserlight; 08-22-2010 at 01:24 PM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Aug 2010
    Posts
    13
    Well, i managed to get the error gone but it didn't work still cause i put the command line in the exe file name and still nothing. this is what i did

    Code:
      
               for(int index = 0; index < argc; ++index)
        {
                if(index == 1)
                {
                     if(strcmp(argv[index], "-Dev")== 0)
                     {
                         cout << "HELLO" << endl;
                     }
                }
        }
    jeez, I can't seem to get it :-p

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,633
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Aug 2010
    Posts
    13
    so a command like arg isn't used like this: app_name -Dev.exe? cause that diodn't work, also, a command line arg is when you would run the command prompt and type that in? again, sorry if i'm not getting it that well, I've used C++ for a long time but i've never tryed to learn to use command line args so i'm just trying to understand

  10. #10
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,230
    O_o

    Code:
    appname.exe --args-here
    or

    Code:
    appname --args-here
    not

    Code:
    appname.exe --args-here.exe
    Soma

  11. #11
    Registered User
    Join Date
    Aug 2010
    Posts
    13
    I tried to rename the file appname.exe -Dev and it changed the extention.......maybe i should wait til i learn this in a class

  12. #12
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,230
    O_O

    Wow.

    You don't rename the file. You execute the application with parameters. This isn't rocket science.

    Soma

  13. #13
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    But...
    I've used C++ for a long time

  14. #14
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    How about beginning with posting a simple program which simply PRINTS all the command line arguments?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  15. #15
    Registered User
    Join Date
    Aug 2010
    Posts
    13
    hehe, sorry i was just trying to do like Half life did in the past.......OH BOY! I'm sorry

Page 1 of 2 12 LastLast
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, 03:25 AM
  2. Parameters quick Question
    By lifeis2evil in forum C++ Programming
    Replies: 2
    Last Post: 11-18-2007, 10: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, 02:13 AM
  5. command-line parameters.
    By Tombear in forum C Programming
    Replies: 2
    Last Post: 10-28-2001, 07:40 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21