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?
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?
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 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.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; }
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?
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
Thanks for your time... hope i did not waste it!Code:if (!strstr(argv[1],password)) //argv[2] above has been changed to argv[1] which pints to the string 123456
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.
I think I have one more question:
If this part is applied the window is closed suddenly. I also tried this one:Code:} else { printf("The password is wrong"); exit(1); } return 0; }
But even this didn't make sense. What can I do to see the output?Code:} else { printf("The password is wrong"); getchar(); exit(1); } return 0; }
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.