应用setjmp,longjmp 密码输入模块

    在《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;

}

 

可以用这个小模块 作为 文件加密程序 或登录操作的一部分。在这里记录下来以后再添加功能

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值