Ciao,
Look at my code below. That`s a standard program for Log in authentication. In the file "Login.txt" located 1 string with user name. When program starts, it asks to enter the user name, then compare this string with the string saved in "Login.txt", if both equal then - success, if not equal, then we can try only 3 times till the program ends; I need to save in "Login.txt" 3 strings (user names) for example: Brad David Peter
So I need to call each string from "Login.txt separately to char buf[], then to call scanf function (David - for example), after use strcmp for both, and as a result both strings should be equal each other;
Question: How to extract multiple strings "separately" and put each to different char buf[] from a single file in C FILE .txt?
Thank you in advance.
CODE:
int main() {
system("cls");
FILE *p;
int n=3,b,v;
char buf[20],buf2[20],Login[20],Password[20];
p=fopen("Login.txt","rt");
fgets(buf,20,p);
fclose(p);
p=fopen("Password.txt","rt");
fgets(buf2,20,p);
fclose(p);
printf("%s",buf);
do {
printf("Please Log in. You have %d tries\n",n);
printf("Enter your Login\n");
scanf("%s",Login);
printf("\nEnter your Password\n");
scanf("%s",Password);
printf("\nCompare Login = %d Compare Login = %d\n",strcmp(buf,Login),strcmp(buf2,Password));
n--;
b=strcmp(buf,Login);
v=strcmp(buf2,Password);
if (v==0&&b==0){
printf("\nYou logged in\n\n");
break;
}
else if (n==0) {
printf("\nYou failed\n\n");
}
}
while (n!=0);
}