im trying to validate user name, mail and password from "users.txt" file, im trying to use fgets, but when I ran it, it wouldnt recognize the information and, when I tried printing what it was actually validating, turned out that the strings it got from the fgets function were totally unexpected.
Data from users.txt:
- jean
- jn@gmail.com
- jj123
- alfredo
- alf@gmail.com
- somepass
- robt
- rob@gmail.com
- rob111
As far as I know, fgets returns the string until it finds a \n character or it reaches the end of the file. what I actually got from printf:
jj123 jj123 jj123 robt robt robt rob111 (null) (null)
Code: `
void login(bool is_logged_in)
{
printf("enter your name\n");
string name_session = GetString();
printf("enter your mail:\n");
string mail_session = GetString();
printf("enter your password:\n");
string password_session = GetString();
FILE *users_file;
char getstring[100];
users_file = fopen("users.txt", "r");
if (users_file != NULL)
{
while (feof(users_file) == 0)
{
//added this to check what I was actually getting from file
string text1 = fgets(getstring,100,users_file);
string text2 = fgets(getstring,100,users_file);
string text3 = fgets(getstring,100,users_file);
printf("%s",text1);
printf("%s",text2);
printf("%s",text3);
//3 times because the if statement is checking 3 strings
if ((fgets(getstring,100,users_file) == name_session ) &&
(fgets(getstring,100,users_file) == mail_session) &&
(fgets(getstring,100,users_file) == password_session))
{
is_logged_in = true;
fclose(users_file);
menu();
}
}
printf("You've failed your log in.\n");
fclose(users_file);
menu();
}
else
{
printf("¡¡ERROR!!: there's been a problem and the program will now exit.\n");
}
}
`