Reputation: 5
I am trying to create a program where one can create an account using recommended information and can store it in a file.
Then there will be another function where it will print those information stored in file. Those information are taken input in SignUp
function.
After saving those information in File, the .txt file information is weird(.txt file image is included).
When I try to call the function Information()
to print the info stored before, either nothing print or some weird text got print instead.(Output image also included).
I hope I made my problem clear and someone can explain me where I made the mistake.
Necessary code given below. I created two function for SignUP
(where it will take necessary info and store them in file), SignIN
to verify my username & password. Information()
function shows the the input taken from SignUP
and print them.
struct information
{
char username[10];
char password[10];
int date, month, year;
int pnumber[20];
char fname[20];
char lname[20];
char fathname[20];
char mothname[20];
char address[50];
};
int main()
{
int i;
printf("1. SIGN IN\n2. SIGN UP\n");
printf("CHOOSE AN OPTION: ");
scanf("%d", &i);
switch(i)
{
case 1:
signin();
case 2:
signup();
}
mainmenu();
}
void info()
{
system("cls");
FILE* fp;
int choice, i;
fp = fopen("username.txt", "rb");
struct information u1;
if (fp == NULL)
{
printf("error in opening file");
}
gotoxy(28, 2);
printf("..........................");
gotoxy(55, 6);
printf("==== YOUR ACCOUNT INFO ====");
gotoxy(55, 8);
printf("***************************");
gotoxy(55, 10);
printf("NAME..%s %s", u1.fname, u1.lname);
gotoxy(55, 12);
printf("FATHER's NAME..%s %s", u1.fathname, u1.lname);
gotoxy(55, 14);
printf("MOTHER's NAME..%s", u1.mothname);
gotoxy(55, 18);
printf("MOBILE NUMBER..%d", u1.pnumber);
gotoxy(55, 20);
printf("DATE OF BIRTH.. %d-%d-%d", u1.date, u1.month, u1.year);
gotoxy(55, 22);
printf("ADDRESS..%s", u1.address);
int choose;
printf("WOULD YOU LIKE TO GO BACK TO MAIN MENU?:\n");
printf("< 1 > YES\n");
printf("< 2 > NO\n");
scanf("%d", &choose);
if (choose == 2)
{
menuExit();
}
else
mainmenu();
}
void signup()
{
char password[20];
int passwordlength, i, seek = 0;
char ch;
FILE *fp, *fu;
struct information u1;
struct information p1;
// Opening file to
// write data of a user
fp = fopen("username.txt", "ab");
system("cls");
printf("\n\n!!!!!CREATE YOUR ACCOUNT!!!!!");
printf("\n\nFIRST NAME...");
scanf("%s", &u1.fname);
printf("\nLAST NAME...");
scanf("%s", &u1.lname);
printf("\nFATHER's NAME...");
scanf("%s", &u1.fathname);
printf("\nMOTHER's NAME...");
scanf("%s", &u1.mothname);
printf("\nADDRESS..");
scanf("%s", &u1.address);
printf("\nDATE OF BIRTH...");
printf("\nDATE-");
scanf("%d", &u1.date);
printf("\nMONTH-");
scanf("%d", &u1.month);
printf("\nYEAR-");
scanf("%d", &u1.year);
printf("\nPHONE NUMBER...");
scanf("%s", u1.pnumber);
printf("\nUSERNAME.. ");
scanf("%s", &u1.username);
printf("\nPASSWORD..");
scanf("&d", &u1.password);
// Taking password in the form of stars
for (i = 0; i < 50; i++)
{
ch = getch();
if (ch != 13)
{
password[i] = ch;
ch = '*';
printf("%c", ch);
}
else
break;
}
fwrite(&u1, sizeof(u1), 1, fp);
fclose(fp);
printf("\n\nACCOUNT CREATED SUCCESSFULLY.\n");
char option[10];
printf("\nPRESS ANY KEY THEN ENTER TO GO TO SIGN IN PAGE");
scanf("%s", &option);
signin();
}
void signin()
{
system("cls");
char username[50];
char password[50];
int i, j, k;
char ch;
FILE *fp, *fu;
struct information u1;
// Opening file of
// user data
fp = fopen("username.txt","rb");
if (fp == NULL)
{
printf("\nERROR IN OPENING FILE\n");
printf("FILE DOESN'T EXIST\nYOU HAVE TO CREATE AN ACCOUNT FIRST\n");
printf("PRESS ANY KEY & ENTER TO CREATE AN ACCOUNT\n");
char option[10];
scanf("%s", &option);
signup();
}
gotoxy(34, 2);
printf(" ACCOUNT LOGIN ");
gotoxy(7, 5);
printf("***********************************************"
"********************************");
gotoxy(35, 10);
printf("==== LOG IN ====");
// Take input
gotoxy(35, 12);
printf("ENTER USERNAME.. ");
scanf("%s", &username);
gotoxy(35, 14);
printf("ENTER PASSWORD..");
// Input the password
for (i = 0; i < 50; i++)
{
ch = getch();
if (ch != 13)
{
password[i] = ch;
ch = '*';
printf("%c", ch);
}
else
break;
}
// Checking if username
// exists in the file or not
while (fread(&u1, sizeof(u1), 1, fp))
{
if (strcmp(username, u1.username) == 0)
{
mainmenu();
}
else
{
printf("\n\n\nINFORMATION INCORRECT. PRESS ANY KEY & ENTER TO TRY AGAIN\n");
char option[10];
scanf("%s", &option);
signin();
}
}
fclose(fp);
}
Upvotes: 0
Views: 77
Reputation: 44246
Well, there are quite many bugs in your code and even more "bad practice" examples. Let's take a few.
scanf("%s", &u1.fname);
This is both wrong and dangerous.
&
shall not be used since fname
is a char array
%s
without a size (width) specification is something that you should never do
The return value from scanf
shall always be checked
So the code should be:
if (scanf("%19s", u1.fname) != 1)
{
// Input error
... add error handling here...
}
Next:
scanf("&d", &u1.password);
Here you use %d
which means "scan for an integer" but password
is a char array. That's obviously a bug.
and
scanf("%s", u1.pnumber);
Here you use %s
which means "scan for characters" but pnumber
is an integer array. That's obviously a bug.
In the function signin
you have code that reads a password. However, your code never ever uses the value that you read. Strange... probably a bug.
Also notice how your password
variables have different sizes. Some times 10, some times 20, sometimes 50. And when it is 20, you still allow the user to input 50 chars. That's bad... very bad.
And there are plenty more.
That said I think your are mainly confused about:
After saving those information in File, the .txt file information is weird
What you are doing is to write a whole struct information
object to a binary file. This means that every single byte of the struct (and any padding it may have) will be written to the file as raw values. For instance int pnumber[20];
will cause the file to contain 20 integers in whatever raw format your system is using.
Such binary files can't be opened in a text editor like notepad. You will just get all kinds of strange symbols.
So your expectation are wrong. Your .txt
are not really text files and can't be read using simple test editors.
That doesn't mean that the file is wrong. The may be fine but opening it in a text editor makes no sense.
Upvotes: 1