在《c专家编程》中看到这个函数,感觉用这个写密码登陆挺合适的。就写个简单的玩玩 代码如下;
#include <stdio.h>
#include <string.h>
#include <setjmp.h>
#include <stdlib.h>
#define staus_ok 2
jmp_buf buf;
unsigned char input_password()
{
static int errorflg=0;
char str[100];
const char password[]="lijunlong";
if(setjmp(buf))
{
printf("the password is wrong,please input the right password: ");
gets(str);/*输入一个字符串*/
errorflg+=1;
}
else
{
printf("please input the password: ");
gets(str);/*输入一个字符串*/
}
if (strcmp(str,password)!=0)
{
if(errorflg<2)
{
longjmp(buf,1);
memset(str,0,sizeof(str));
}
else
{
printf("error:you have input the wrong password three times ");
exit(EXIT_FAILURE);
}
}
else
{
printf("your password is ok/n");
return staus_ok;
}
return staus_ok;
}
int main()
{
unsigned char password_staus;
password_staus=input_password();
if(password_staus==staus_ok)
{
/****其他处理函数****/
}
else
{
exit(EXIT_FAILURE);
getchar();
}
printf("the software designed by jldream,thank you for use it/n");
getchar();
return 1;
}
可以用这个小模块 作为 文件加密程序 或登录操作的一部分。在这里记录下来以后再添加功能