C++写一个带参数运行的程序
用C++写个例子,要求实现带参数运行。如:test.exe/user:someone/pwd:password要求只有用户和密码通过验证才执行程序,否则提示终止。...
用C++写个例子,要求实现带参数运行。
如:test.exe /user:someone /pwd:password
要求只有用户和密码通过验证才执行程序,否则提示终止。 展开
如:test.exe /user:someone /pwd:password
要求只有用户和密码通过验证才执行程序,否则提示终止。 展开
6个回答
推荐于2016-01-22
展开全部
#include <string.h>
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char **argv)
{
if (argc < 3)
{
cout << "Usage : test.exe /user:someone /pwd:password" << endl;
exit(-1);
}
const char *user = "someone";
const char *pswd = "something";
char *u = argv[1], *p = argv[2];
while (*u++ != ':');
while (*p++ != ':');
if (strcmp(u, user) || strcmp(p, pswd))
{
cout << "User name or Password invalid! exiting.." << endl;
exit(-1);
}
cout << "Hello, " << user << endl;
system("PAUSE");
return 0;
}
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char **argv)
{
if (argc < 3)
{
cout << "Usage : test.exe /user:someone /pwd:password" << endl;
exit(-1);
}
const char *user = "someone";
const char *pswd = "something";
char *u = argv[1], *p = argv[2];
while (*u++ != ':');
while (*p++ != ':');
if (strcmp(u, user) || strcmp(p, pswd))
{
cout << "User name or Password invalid! exiting.." << endl;
exit(-1);
}
cout << "Hello, " << user << endl;
system("PAUSE");
return 0;
}
展开全部
用户名和密码的定义可以在
#define USERID "tom"
#define PASSWORD "tom@1234"
这里修改
附上代码和执行结果
#include<stdio.h>
#include<string.h>
#define USERID "tom"
#define PASSWORD "tom@1234"
int main(int argc,char *argv[])
{
if(argc == 1)
{
printf("error: Valid ID and Password are required\nfor example: test.exe jack jack@1234\n");
return 0;
}
else if(argc == 3)
{
if (strcmp(argv[1],USERID) != NULL || strcmp(argv[2],PASSWORD) != NULL)
{
printf("error: Valid ID and Password are required\nfor example: test.exe jack jack@1234\n");
return 0;
}
}
printf("Authentication Pass..\n");
return 0;
}
执行结果
C:\Documents and Settings\ljlu\桌面\vctest\Debug>Text1.exe tom tom@1234
Authentication Pass..
C:\Documents and Settings\ljlu\桌面\vctest\Debug>Text1.exe tom tom@123
error: Valid ID and Password are required
for example: test.exe jack jack@1234
C:\Documents and Settings\ljlu\桌面\vctest\Debug>Text1.exe tm tom@1234
error: Valid ID and Password are required
for example: test.exe jack jack@1234
C:\Documents and Settings\ljlu\桌面\vctest\Debug>Text1.exe
error: Valid ID and Password are required
#define USERID "tom"
#define PASSWORD "tom@1234"
这里修改
附上代码和执行结果
#include<stdio.h>
#include<string.h>
#define USERID "tom"
#define PASSWORD "tom@1234"
int main(int argc,char *argv[])
{
if(argc == 1)
{
printf("error: Valid ID and Password are required\nfor example: test.exe jack jack@1234\n");
return 0;
}
else if(argc == 3)
{
if (strcmp(argv[1],USERID) != NULL || strcmp(argv[2],PASSWORD) != NULL)
{
printf("error: Valid ID and Password are required\nfor example: test.exe jack jack@1234\n");
return 0;
}
}
printf("Authentication Pass..\n");
return 0;
}
执行结果
C:\Documents and Settings\ljlu\桌面\vctest\Debug>Text1.exe tom tom@1234
Authentication Pass..
C:\Documents and Settings\ljlu\桌面\vctest\Debug>Text1.exe tom tom@123
error: Valid ID and Password are required
for example: test.exe jack jack@1234
C:\Documents and Settings\ljlu\桌面\vctest\Debug>Text1.exe tm tom@1234
error: Valid ID and Password are required
for example: test.exe jack jack@1234
C:\Documents and Settings\ljlu\桌面\vctest\Debug>Text1.exe
error: Valid ID and Password are required
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2009-06-19
展开全部
把用户名和密码写在命令行参数是非常有安全隐患的做法。比如将密码写在脚本文件中,容易被别人攻破而被读取。历史上这么做曾导致了惨重的代价。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
int main(int argc,char *argv[])
argc表示命令行参数的个数.
argv表示一个字符串数组,是所有的命令行参数.
不过默认的有一个参数,就是你当前执行的程序的路径.
所以你传入的参数从argv[1]开始.
argc表示命令行参数的个数.
argv表示一个字符串数组,是所有的命令行参数.
不过默认的有一个参数,就是你当前执行的程序的路径.
所以你传入的参数从argv[1]开始.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char* argv[])
{
if(strcmp(argv[1],"/user:someone")==0 && strcmp(argv[2],"/pwd:password")==0)
printf("hello world");
else
printf("wrong!");
return 0;
}
#include <stdlib.h>
#include <string.h>
int main(int argc, char* argv[])
{
if(strcmp(argv[1],"/user:someone")==0 && strcmp(argv[2],"/pwd:password")==0)
printf("hello world");
else
printf("wrong!");
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询