Thread: Command Line Arguments

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    113

    Command Line Arguments

    Hi everybody,

    I want to see how the command line arguments work. However I don't know how to enter this input. Could you please show me a method to see if my code works?

  2. #2
    Registered User
    Join Date
    Feb 2013
    Location
    Buea Cameroon
    Posts
    64
    Quote Originally Posted by GokhanK View Post
    Hi everybody,

    I want to see how the command line arguments work. However I don't know how to enter this input. Could you please show me a method to see if my code works?
    Where is the code?

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    113
    Code:
    #include <stdio.h>
    #include <string.h>
    int main(int argc, char* argv[]){
    if (argc<2)
    printf("Please enter a password\n");
    char password[]="123456";
    if (!strstr(argv[2],password))
    printf("Wellcome!\n");
    else{
    printf("The password is wrong");
    exit(1);
    }
    return 0;
    }
    I want the user to use a password and want to put it in argv[]. However I don't know where the user will enter the password.

  5. #5
    Registered User
    Join Date
    Feb 2013
    Location
    Buea Cameroon
    Posts
    64
    Quote Originally Posted by GokhanK View Post

    I want the user to use a password and want to put it in argv[]. However I don't know where the user will enter the password.
    did you read the previous link provided by salem? FAQ > Accessing command line parameters/arguments - Cprogramming.com
    Assuming you did which OS and compiler or IDE are you using for your C programming?

  6. #6
    Registered User
    Join Date
    Dec 2010
    Posts
    113
    Quote Originally Posted by acho.arnold View Post
    did you read the previous link provided by salem? FAQ > Accessing command line parameters/arguments - Cprogramming.com
    Assuming you did which OS and compiler or IDE are you using for your C programming?
    Yes I have read it. However I couldn't see where I should enter the password or any argument. I am using Code::Block on Windows 8.

    Edit: I also tried opening the exe as C: ... Untitled1.exe 123456
    Last edited by GokhanK; 08-17-2013 at 03:24 PM.

  7. #7
    Registered User
    Join Date
    Feb 2013
    Location
    Buea Cameroon
    Posts
    64
    Okey first of all its good programming practice to indent the code it makes it easy to read and in this case debug
    considar the indentd version of the above code below
    Code:
    #include <stdio.h>
    #include <string.h>
    #include<stdlib.h>
    
    
    int main(int argc, char* argv[])
    {
    	if (argc<2)
    	{
    		printf("Please enter a password\n");
    	}		
    	char password[]="123456";
    	
    	if (!strstr(argv[2],password))
    	{
    		printf("Wellcome!\n");
    	}
    	else
    	{
    		printf("The password is wrong");
    		exit(1);
    	}
    	return 0;
    }

    when you compile the code in the command prompt open it with C: ... Untitled1.exe 123456 and i think it will work this time
    The arguement is always passed during the execution
    The reason why it didn't work was because
    argv[0] points to the path where the executable is stored and
    argv[1] pointes to the arguement in this case - 123456


    so you were to change line 7 of the above code to
    Code:
    if (!strstr(argv[1],password)) //argv[2] above has been changed to argv[1] which pints to the string 123456
    Thanks for your time... hope i did not waste it!

  8. #8
    Registered User
    Join Date
    Dec 2010
    Posts
    113
    Thank you for your time. It is now clear to me. As I am an old MATLAB user I am always doing this error. Thanks again for your help.

  9. #9
    Registered User
    Join Date
    Dec 2010
    Posts
    113
    I think I have one more question:

    Code:
        }
        else
        {
            printf("The password is wrong");
            exit(1);
        }
        return 0;
    }
    If this part is applied the window is closed suddenly. I also tried this one:

    Code:
        }
        else
        {
            printf("The password is wrong");
            getchar();
            exit(1);
        }
        return 0;
    }
    But even this didn't make sense. What can I do to see the output?

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    If you are using code blocks, you should be running the executable in the IDE. Under the "Project" menu there is an option that says "Set program's arguments". Use that.

    Using the getchar() function to keep the window open should work, but the stdin stream has to be empty, otherwise getchar() will just extract something that the user typed earlier, like the \n from the enter key. Read SourceForge.net: Clearing the input buffer - cpwiki. If you start using the IDE to set program arguments though, this becomes a non-issue, as the IDE will run your executable and close the window when you tell it to close.

    Upon release, you can use bat files (or something else clever) to run the program in a console window.
    Last edited by whiteflags; 08-17-2013 at 10:06 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. command line arguments
    By stefanyco in forum C++ Programming
    Replies: 25
    Last Post: 08-04-2011, 03:58 AM
  2. Command Line arguments
    By sidhuram in forum Windows Programming
    Replies: 2
    Last Post: 09-06-2010, 08:49 AM
  3. [C++] Command Line Arguments help
    By camelCase in forum C++ Programming
    Replies: 10
    Last Post: 07-03-2010, 08:18 AM
  4. command line arguments
    By tempster09 in forum C Programming
    Replies: 3
    Last Post: 12-06-2009, 06:27 PM
  5. Command Line Arguments???
    By luckygold6 in forum C++ Programming
    Replies: 8
    Last Post: 04-28-2003, 05:51 PM