【发布时间】:2016-05-11 04:32:43
【问题描述】:
我正在编写一个用户验证程序,成功登录后,我提供了一个类似 shell 的提示:
int main()
{
int argc;
char **argv;
char *username = malloc(50);
char *passwd = malloc(32);
char command[200];
printf("Enter username:");
scanf("%s", username);
printf("\n");
printf("Enter password:");
get_passwd(passwd); //User defined function for taking the password without echoing.
// Then a authentication module is called;
int success = authenticate_user(username, passwd);
//After successful authentication of user I will print like:
printf("test @ %s >", username);
// To look user that he is working on program's own terminal where user can input some command as string
gets(command);
//Here is the main problem
// I tried using strtok() and g_shell_parse_argv () but not getting the desired result.
return 0;
}
其他团队成员根据命令字符串解析编写了进一步操作的程序。他们表示我必须知道如何将其解析为 argc 和 argv 变量,就像在 main(int argc, int **argv) 函数签名形式变量中一样。
是否有任何 C 库可以执行此操作,或者有任何最佳实践提示或示例可以继续。
最后我要在 argc 和 argv 中解析一个字符串。
非常感谢任何帮助。 谢谢。
【问题讨论】:
-
请展示您的研究成果。请先阅读How to Ask页面。
-
argc和argv是main函数的参数,由操作系统在程序运行时填写。在您的代码中,它们是未初始化的局部变量。您希望他们如何保存合理的数据?仅仅将它们命名为argc和argv并不会让它们神奇地引用命令行参数。 -
请注意,
main的原型是int main(int argc, char **argv);。在你的问题中你说int **argv