Password Login-function function
#include"stdafx.h"#include<stdio.h>#include<stdlib.h>#include<string.h>#include<conio.h>//contains Getch (); reads a character from the console but does not appear on the screen#pragmaWarning (disable:4996)//The IDE By default disables this vulnerability-prone old function, such as fopen (), now requires the use of fopen_s, to use the old function, add this sentence//Check if it's the first time you've entered the systemintCheckfirst () {inti; Charpwd_first[9];//new system, first input password array Charpwd_second[9];//new system, password array for the second input Charpassword_input[9];//An array of officially entered passwords Charpassword[9];//An array of officially entered passwordsFILE*fp_read,*Fp_write; Fp_read= fopen ("Password.dat","RB"); if(!fp_read)//if Password.dat is not present, enter the IF{printf ("\ n The new system, please enter the appropriate initialization action!\n"); //GetChar (); Do{printf ("\ n Set the password, please do not exceed 8 bits:"); //Getch ()-Reads a character from the console but does not appear on the screen//The CR symbol ' \ r ' indicates that the decimal ASCII code is 13 and the hexadecimal code is 0x0d; //lf using the ' \ n ' notation, the ASCII code is 10, and the 16 system is 0x0a. for(i =0; I <8&& (Pwd_first[i] = Getch ())! ='\ r'; i++) {Putchar ('*'); } Pwd_first[i]=' /';//do not add ' \ ', just a character array, cannot be compared using strcmpprintf ("\ n Please set the password again, please do not exceed 8 bits:"); for(i =0; I <8&& (Pwd_second[i] = Getch ())! ='\ r'; i++) {Putchar ('*'); } Pwd_second[i]=' /'; if(strcmp (pwd_first,pwd_second) = =0) {printf ("\ nthe password entered successfully! \ n"); Break;//Jump out of the loop } Else{printf ("\n2 password inconsistent, please re-enter!\n"); System ("PAUSE");//PauseSystem"CLS");//Clear Screen } } while(1); //fclose (fp_read);//error, cannot be closed first, or Password.dat will fail after opening//write the password to the password fileFp_write = fopen ("Password.dat","WB"); if(!fp_write) {printf ("\ nthe password file failed to open, system creation failed!\n"); return-1; } I=0; while(Pwd_first[i]) {PUTW (pwd_first[i], fp_write);//PUTW is a function that writes a single characteri++; } fclose (Fp_write); printf ("\ nthe system initialization succeeds, press any key to exit, then re-enter!\n"); Exit (0); } Else{i=0; while(!feof (fp_read) && i<8)//determine if you are finished reading the password file{Pwd_first[i]= GETW (Fp_read);//reads the word assigned value from the file stream to the Pwd_first arrayi++; } pwd_first[i-1] =' /';//use feof () read the contents of the file, will always read more once, if the number of characters is 3, it will be read 4 times, i==4, so I want to subtract 1 intn =0; Do{printf ("\ n Please enter your password, please do not exceed 8 bits:"); for(i =0; I <8&& (Password[i] = Getch ())! ='\ r'; i++) {Putchar ('*'); } Password[i]=' /'; if(strcmp (password, pwd_first)! =0) {printf ("\ nthe password is wrong, please re-enter!\n"); N++; System ("PAUSE");//PauseSystem"CLS");//Clear Screen } Else{System ("CLS");//Clear Screenprintf"password correct!\n"); System ("PAUSE");//PauseSystem"CLS");//Clear Screen Break;//password is correct, exit function, continue execution down } } while(n<3); if(N >=3) {printf ("Please exit, you have entered three times the wrong password!\n"); Exit (1); } } return 0;}int_tmain (intARGC, _tchar*argv[]) {Checkfirst (); //Check if it's the first time you've entered the systemprintf ("\ n successfully entered the system! \ n"); return 0;}
Enter the new system for the first time:
Second enter the system, enter the password, the password is correct:
Common function functions-Password login function