Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to? command line arguments 2

Status
Not open for further replies.

bigtamscot

Programmer
Apr 28, 2001
37
GB
Hi,

I have reached the stage in learning where I have come across Command Line arguments in main ( ). I can write the coding but donot know how to invoke the command line arguments for the program ?????? The truth is I wouldn't know where to start. Operating system is Windows 98, compiler is DevC++. Below is a sample program for password where the users password is passed to main in command line arguments. The program compiles and runs ok (without command entries). But how do I enter a password, e.g. Command Line argument. Every time I run the program I get message incorrect number of parameters, follow program name with parameters. HOW do I do this.

#include <stdio.h>
#include <string.h>

int main (int argc, char *argv [])
{
char password [20] = &quot; c0mmander21 &quot; ;
int fin;

printf(&quot; Logon in Progress \n &quot;) ;

if(argc!= 2)
{
printf(&quot;Incorrect number of parameters \n&quot;
&quot;Follow program name with parameters \n &quot;);
return ;
}
if(!strcmp(password, argv[1]))
printf(&quot;Logon Complete...&quot;);
else
{
printf(&quot;Incorrect Password... Logon Failed \n&quot;);
return ;
}
printf(&quot;Welcome to program ?!!&quot;);
}

THERE IS NOTHING WRONG WITH CODE, JUST NEED TO KNOW HOW TO ENTER THE PASSWORD TO PASS TO MAIN ( ). Hoping to get certified..in C programming.
 
The program looks fine... i dont understand why you are not seeing the desired results. Did you try this?

C:\<path>\programName c0mmander21

Matt
 
You initialize password with c0mmander21 with a space on either side of word. Try invoking the program like this:

$ ./prog_name ' c0mmander21 '

Or maybe surrounding it with double quotes depending on your command interpreter.

As an aside, this isn't secure as the password will be embedded in the final executable so someone can easily see it by using a hex editor or using a utility like the strings command found commonly on unixes.
Russ
bobbitts@hotmail.com
 
Thanks guys, problem now solved. rbobbit, program wasnot meant for security, just to see if I could invoke command programs, but i get the jest of what you mean. Hoping to get certified..in C programming.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top