Mitkashin Mitkashin - 1 month ago 20
C Question

user input into File C Programming

#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:


  1. 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 )

  2. 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.

Answer

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);
}