常用功能函数-密码登陆函数

 

密码登陆-功能函数

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h> //包含getch(); 从控制台读取一个字符,但不显示在屏幕上

#pragma warning(disable:4996) //IDE默认禁止这种容易产生漏洞的旧函数,例如fopen(),现在需要使用fopen_s,要想使用旧函数,要加上这句

//检查是否是第一次进入系统
int checkFirst()
{
    int i;
    char pwd_first[9]; //新系统,第一次输入的密码数组
    char pwd_second[9]; //新系统,第二次输入的密码数组
    char password_input[9]; //正式输入的密码数组
    char password[9]; //正式输入的密码数组

    FILE *fp_read,*fp_write;
    fp_read = fopen("password.dat", "rb");
    if(!fp_read) //如果不存在password.dat,则进入if
    {
        printf("\n新系统,请输入相应的初始化操作!\n");
        //getchar();
        do 
        {
            printf("\n设置密码,请不要超过8位:");
            //getch() - 从控制台读取一个字符,但不显示在屏幕上    
            //CR用符号'\r'表示, 十进制ASCII代码是13, 十六进制代码为0x0D;
            //LF使用'\n'符号表示, ASCII代码是10, 十六制为0x0A.
            for (i = 0; i < 8 && (pwd_first[i] = getch()) != '\r';i++) 
            {
                putchar('*');
            }
            pwd_first[i] = '\0'; //不加'\0',只是字符数组,不能使用strcmp进行比较

            printf("\n请再次设置密码,请不要超过8位:");
            for (i = 0; i < 8 && (pwd_second[i] = getch()) != '\r'; i++)
            {
                putchar('*');
            }
            pwd_second[i] = '\0';

            if ( strcmp(pwd_first,pwd_second) == 0 )
            {
                printf("\n密码输入成功! \n");
                break; //跳出循环
            }
            else
            {
                printf("\n2次密码不一致,请重新输入!\n");
                system("PAUSE"); //暂停
                system("CLS"); //清屏
            }

        } while (1);

        //fclose(fp_read); //error,不能先关闭,否则后面打开password.dat会失败

        //将密码写入到密码文件中
        fp_write = fopen("password.dat", "wb");
        if (!fp_write)
        {
            printf("\n密码文件打开失败,则系统创建失败!\n");
            return -1;
        }

        i = 0;
        while (pwd_first[i])
        {
            putw(pwd_first[i], fp_write); //putw为写入单个字符的函数
            i++;
        }
        fclose(fp_write);
        printf("\n系统初始化成功,按任意键退出后,再重新进入!\n");
        exit(0);
    }
    else
    {
        i = 0;
        while ( !feof(fp_read) && i<8 ) //判断是否读完密码文件
        {
            pwd_first[i] = getw(fp_read); //从文件流中读出字符赋值给pwd_first数组
            i++;
        }
        pwd_first[i-1] = '\0'; //使用feof()读取文件内容,总是会多读取一次,假如字符个数为3,则会读取4次,i==4,所以i要减1

        int n = 0;
        do 
        {
            printf("\n请输入密码,请不要超过8位:");
            for (i = 0; i < 8 && (password[i] = getch()) != '\r'; i++)
            {
                putchar('*');
            }
            password[i] = '\0';

            if (strcmp(password, pwd_first) != 0)
            {
                printf("\n密码错误,请重新输入!\n");
                n++;
                system("PAUSE"); //暂停
                system("CLS"); //清屏
            }
            else
            {
                system("CLS"); //清屏
                printf("密码正确!\n");
                system("PAUSE"); //暂停
                system("CLS"); //清屏
                break; //密码正确,退出函数,继续向下执行
            }

        } while(n<3);

        if(n >= 3)
        {
            printf("请退出,你已经输入三次错误密码!\n");
            exit(1);
        }

    }

    return 0;
}

int _tmain(int argc, _TCHAR* argv[])
{
    checkFirst();    //检查是否是第一次进入系统

    printf("\n成功进入系统!\n");
    return 0;
}

 

第一次进入新系统:

 

 第二次进入系统,输入密码,密码正确:

 

posted @ 2018-10-04 13:12  fengMisaka  阅读(1008)  评论(0编辑  收藏  举报