1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
# include <stdio.h>
# include <conio.h>
# include <stdlib.h>
# include <string.h>
/// This function does the folowing tasks
/// 1: Checks if the file exists
/// 2: Gets The raw lines
/// 3: extracts raw ID and PASSWORD
/// I let errors handling to you ,now there is a standard
/// the first line containes the id the second containes the PASSWORD
void get_ID_and_PASS(char fileName[30],char *id,char *pass)
{
FILE *F = fopen(fileName,"r");
if(F)// 1
{
int count = 0;
while(!feof(F))
{
char rawLine[50];
fscanf(F,"%s",rawLine);// 2
if(!count++)// 3
strcpy(id,rawLine);
else
strcpy(pass,rawLine);
}
}else printf("Cannot open this file");
fclose(F);
}
///
int main(void)
{
char fileName[30] = "IDs_file.txt";
char userID[20],userPassW[20];
char strID[30]="\0",strPASSW[30]="\0";
char IDpref[20] = "id:\0",PASSWpref[20] = "pass:\0";
get_ID_and_PASS(fileName,userID,userPassW);
int chances = 3;
char cChoice;
printf("Do you want to register y/n -> ");
cChoice = getch();
printf("\n");
if(cChoice=='Y'||cChoice=='y')
{
FILE *F = fopen(fileName,"w");
printf("Enter your new ID -> ");
scanf("%s",strID);
fprintf(F,"id:%s\n",strID);
printf("Enter your new PASSWORD -> ");
scanf("%s",strPASSW);
fprintf(F,"pass:%s",strPASSW);
fclose(F);
}
while (chances)
{
printf("Enter User ID -> ");
scanf("%s",strID);
printf("\n");
printf("Enter Your Password -> ");
scanf("%s",strPASSW);
strcpy(strID,strcat(IDpref,strID));
strcpy(strPASSW,strcat(PASSWpref,strPASSW));
if (!strcmp(strID,userID)&&!strcmp(strPASSW,userPassW))
{
printf("Correct User ID And Password\n");
printf("\n\nDo something here!\n\n"); ///DO SOMETHING HERE!
break;
}
else
{
printf("Invalid User ID And Password\n\n");
chances--;
system("cls");
if(!chances)
{
printf("You have no more chances...\n");
printf("Do you want to register y/n -> ");
cChoice = getch();
printf("\n");
if(cChoice=='Y'||cChoice=='y')
{
FILE *F = fopen(fileName,"w");
printf("Enter your new ID -> ");
scanf("%s",strID);
fprintf(F,"id:%s\n",strID);
printf("Enter your new PW -> ");
scanf("%s",strPASSW);
fprintf(F,"pass:%s",strPASSW);
fclose(F);
break;
}
}
}
}
printf("press any key to continue");
getch();
}
|