I've made my code working ( closely to working ) but i've a problem. My registration work but the Login part works (reads) only for the First line... Let's say i have my file like this (each account for each line):
user1 pass1
user2 pass2
user3 pass3
user4 pass4
it will grant me a access only if i type the username as user1 and password as pass1, but not for user2 and pass2. how can i fix this ??? CODE:
#include <stdio.h>
#include <stdlib.h>
#include "rlutil.h"
void Login();
void Register();
char username[32];
char password[32];
char acc[32];
char pw[32];
int main()
{
int logreg;
printf("Press '1' For Login");
printf("Press '2' For Register\n");
logreg = getch();
printf("\n------------------------------------------------\n");
tester:
;
if (logreg == '1')
{
Login();
}
else if (logreg == '2')
{
Register();
}
else
{
printf("\nInvalid Input!!! Choose between '1' or '2' !!!\n");
logreg = getch();
goto tester;
}
return 0;
}
void Login()
{
start:
;
char answer;
// Vnesuvanje na username
printf("\nEnter your Username: ");
scanf("%s",username);
// Vnesuvanje na Password
printf("\nEnter your Password: ");
scanf(" %s",password);
FILE *fData;
// Otvara file za citanje
fData = fopen("database.txt", "rt");
if (!fData)
{
printf("The file can not be opened\n\a\a");
}
int found=0;
while(!feof(fData) && !found)
{
fscanf(fData, "%s\t%s", acc, pw);
if (strcmp(username, acc) == 0 && strcmp(password, pw) == 0)
{
setColor(LIGHTGREEN);
printf("\nSuccessfuly logged it to our WebSite\n\n");
setColor(GREY);
found = 1;
getch();
break;
}
else if (!found)
{
setColor(LIGHTRED);
printf("\nNo Access to our WebSite\n\n");
printf("Invalid username or password!!!\n\n");
setColor(GREY);
printf("Would you like to try again?? [y/n] ");
answer = getch();
found = 0;
printf("\n\n------------------------------------------------\n");
break;
}
}
fclose(fData);
tester2:
;
//proverka za Povtorno pustanje na programata
if (answer== 'y')
{
goto start;
}
else
{
if (answer!='n')
{
printf("Please choose between 'y' or 'n' !!!\n\n");
answer = getch();
goto tester2;
}
else
{
getch();
return 0;
}
}
}
void Register()
{
char acc[32];
char pw[32];
FILE *fData;
fData = fopen("database.txt", "a");
if (!fData)
{
printf("File could not be opened\n\a\a");
getchar();
return;
}
printf("Enter your desired Username: ");
scanf("%s", acc);
printf("Enter your desired Password: ");
scanf("%s", pw);
printf("\n");
fprintf(fData, "%s\t%s\n", acc, pw);
fclose(fData);
}
the problem should be somewhere here:
FILE *fData;
// Otvara file za citanje
fData = fopen("database.txt", "rt");
if (!fData)
{
printf("The file can not be opened\n\a\a");
}
int found=0;
while(!feof(fData) && !found)
{
fscanf(fData, "%s\t%s", acc, pw);
if (strcmp(username, acc) == 0 && strcmp(password, pw) == 0)
{
setColor(LIGHTGREEN);
printf("\nSuccessfuly logged it to our WebSite\n\n");
setColor(GREY);
found = 1;
getch();
break;
}
else if (!found)
{
setColor(LIGHTRED);
printf("\nNo Access to our WebSite\n\n");
printf("Invalid username or password!!!\n\n");
setColor(GREY);
printf("Would you like to try again?? [y/n] ");
answer = getch();
found = 0;
printf("\n\n------------------------------------------------\n");
break;
}
}
fclose(fData);
but i dont know how do i fix the code, if some one who can fix it and paste it in a comment ( answer ) i would be so thankful. Anyway, thank you very much PS. Dont mind me, im not that good at programming but still willing to learn :)