#include <stdio.h>
#include <stdlib.h>
#include "rlutil.h"
void Login();
void Register();
char username[32];
char password[32];
int main()
{
int logreg;
printf("Press '1' for Login! \n");
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!!! Chose 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);
// Proverka na username i password dali se tocni ili ne!!
if (strcmp(username, "igor.mite") == 0 && strcmp(password, "igormite") == 0)
{
setColor(LIGHTGREEN);
printf("\nSuccessfully connected to our WebSite\n\n");
setColor(GREY);
getch();
return 0;
}
else
{
setColor(LIGHTRED);
printf("\nNo Access on our WebSite\n\n");
printf("Invalid username or password!!!\n\n");
setColor(GREY);
printf("Would you like to try again ?? [y/n]");
answer = getch();
printf("\n------------------------------------------------\n");
}
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;
}
}
return;
}
void Register(){
char acc[32];
char * user;
FILE * fData;
fData = fopen("database.txt", "a");
printf("\nEnter your desired Username: ");
user = scanf(" %s",acc);
fprintf(fData, &user, stdin);
return;
}
Before I start I wanna say I'm not really good at programming and my English is not that good. Here is my code. We will go step by step how does my code work:
Asking if you want to login or register! ( The login part works good except I want to get the users info from a text document )
Now here comes the problem. I don't know how do I make my input into the file
Let's say I want to register a new Account with the name "Admin". It crashes my program.
If you can tell me how do I do my code in the function Register()
, or even better if you can tell me how I can fix it, please do.
Thank you, There are some comments in my language soo... don't look at them :D
It's maybe not the most complex code but for a beginner don't mind me.
EDIT: I've made my code working ( closely to working ) but i've another problem. My registration work but the Login part works only for the First line... Let's say i have my file like this: 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;
}
}
}
{
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);
}
答案 0 :(得分:0)
Posted OP's commented solution as a candidate.
void Register() {
FILE *fData;
int i;
char acc[32];
char pw[32];
int found = 0;
fData = fopen("database.txt", "wt");
if (!fData) {
printf("File could not be opened\n\a\a");
getchar();
// return -1;
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);
}