我的银行记录管理程序中的 fread 和 fwrite 出现问题
我正在做我的银行记录管理系统项目,在读取文件时遇到问题。我的文件名是“name_of_the_user.txt”并以wb+模式打开。我有一个结构,其中包含一些字段,例如名称、地址、金钱等。我一直在尝试通过将数据读入结构 BMS u1 来存款和取款,然后执行以下功能,但我认为读取此结构会导致文件损坏和一些垃圾值。请帮忙。
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
void main_menu(char*);
void update_acc(char *);
// void deposit( char *);
// void withdrawl( char *);
struct BMS
{
char name[20];
char mailid[20];
unsigned long long contact;
unsigned long long adhar;
int date, year, month;
char address[100];
char password[15];
int money;
};
struct balance{
char namefrom[20];
char nameto[20];
long int money;
};
void gotoxy(int x, int y)
{
COORD c;
c.X = x;
c.Y = y;
SetConsoleCursorPosition(
GetStdHandle(STD_OUTPUT_HANDLE), c);
}
void view_acc(char username[])
{
FILE *fp;
struct BMS u1;
char filename[20];
strcpy(filename, username);
fp = fopen(strcat(filename, ".bin"), "rb");
if(fp==NULL){
printf("Error viewing account details.");
}
fread(&u1, sizeof(u1), 1, fp);
printf("%s\n", u1.name);
printf("%llu\n", u1.contact);
printf("%llu\n", u1.adhar);
printf("%d - %d - %d\n", u1.date, u1.month, u1.year);
printf("%s\n", u1.address);
printf("%s\n", u1.mailid);
printf("Balance : %d\n", u1.money);
fclose(fp);
}
void create_acc()
{
system("cls");
FILE *fp;
struct BMS u1;
int k=0;
char ch1, temp, filename[20];
scanf("%c",&temp);
u1.money = 0;
printf("\t\t\tFill in the details \n");
printf("Name of account holder: ");
scanf("%[^\n]", &u1.name);
printf("Enter DOB : ");
printf("Date : ");
scanf("%d" , &u1.date);
printf("\nMonth : ");
scanf("%d", &u1.month);
printf("\nYear : ");
scanf("%d", &u1.year);
printf("Enter your mobile number : ");
scanf("%llu", &u1.contact);
printf("Enter your Aadhar ID : ");
scanf("%llu", &u1.adhar);
scanf("%c",&temp);
printf("Enter user email : ");
scanf("%[^\n]", &u1.mailid);
scanf("%c",&temp);
printf("Enter Your Address : ");
scanf("%[^\n]", &u1.address);
printf("Create PIN : ");
for(k=0; k<15; k++)
{
ch1 = getch();
u1.password[k]=ch1;
if(ch1!=13 && ch1!=8)
printf("*");
if(ch1 == 13)
break;
}
u1.password[k]='\0';
printf("\nYour created PIN is : ");
for(k=0; k<strlen(u1.password); k++){
printf("%c", u1.password[k]);
}
printf("\nYour Account has beem succesfully added to our system. Please remember your created PIN to acces your account.\n\n");
strcpy(filename, u1.name);
fp = fopen(strcat(filename, ".bin"), "ab");
fwrite(&u1, sizeof(u1),1, fp);
fclose(fp);
}
void login(){
system("cls");
FILE *fp;
struct BMS u1;
char name[20], temp, password[15],ch1, filename[20];
int i, j, k;
scanf("%c",&temp);
printf("Enter your name : ");
scanf("%[^\n]", &name);
scanf("%c",&temp);
printf("Enter your PIN : ");
scanf("%s", &password);
strcpy(filename, name);
fp = fopen(strcat(filename, ".bin"),"rb");
if(fp==NULL){
printf("No account found.");
}
fread(&u1, sizeof(u1), 1, fp);
if(!strcmp(password, u1.password)){
printf("Login succesful.");
fclose(fp);
main_menu(name);
}
else {
printf("Login failed wrong password.");
fclose(fp);
}
}
void transaction(char username[])
{
system("cls");
FILE *fp, *fp1, *fm;
struct BMS u1;
struct balance b1;
char userfrom[20], userto[20],username1, username2, temp;
// strcpy(username1, username);
scanf("%c", &temp);
printf("Transfer money to : ");
scanf("%[^\n]", &userto);
printf("Enter the amount to be transferred : ");
scanf("%ld", &b1.money);
// fp1 = fopen(strcat(username1, ".txt"), )
strcpy(userfrom, username);
strcpy(username, userto);
fp = fopen(strcat(username, ".bin"), "rb");
fm = fopen("money.bin", "ab");
while (fread(&u1, sizeof(u1), 1, fp))
{
if (strcmp(userto, u1.name) == 0) {
strcpy(b1.nameto, u1.name);
strcpy(b1.namefrom, userfrom);
}
}
fwrite(&b1, sizeof(b1), 1, fm);
fclose(fm);
fclose(fp);
}
void viewbalance(char username[]){
system("cls");
FILE *fm;
struct balance b1;
char ch;
int i=1 , money=0;
fm = fopen("money.bin", "rb");
int k = 5, l = 10;
int m = 30, n = 10;
int u = 60, v = 10;
gotoxy(30, 2);
printf("==== BALANCE DASHBOARD ====");
gotoxy(30, 3);
printf("***************************");
gotoxy(k, l);
printf("S NO.");
gotoxy(m, n);
printf("TRANSACTION ID");
gotoxy(u, v);
printf("AMOUNT");
while (fread(&b1, sizeof(b1), 1, fm)) {
if (strcmp(username, b1.nameto) == 0) {
gotoxy(k, ++l);
printf("%d", i);
i++;
gotoxy(m, ++n);
printf("%s", b1.namefrom);
gotoxy(u, ++v);
printf("%ld", b1.money);
money = money + b1.money;
}
}
gotoxy(80, 10);
printf("TOTAL AMOUNT");
gotoxy(80, 12);
printf("%d", money);
getch();
fclose(fm);
}
void deposit( char username[]){
struct BMS u1;
FILE *fp;
char filename[20];
strcpy(filename, username);
fp = fopen(strcat(filename, ".bin"), "wb+");
fread(&u1, sizeof(u1),1, fp);
int amount = 0;
printf("Enter the amount to be deposited : ");
scanf("%d", &amount);
u1.money = u1.money + amount;
printf("Money succesfully deposited.");
fseek(fp, 0, SEEK_SET);
fwrite(&u1, sizeof(u1),1, fp);
fclose(fp);
}
void withdrawl( char username[]){
struct BMS u1;
FILE *fp;
char filename[20];
strcpy(filename, username);
fp = fopen(strcat(filename, ".bin"), "wb+");
if(fp==NULL){
printf("Error");
}
printf("%s", u1.name);
if(fread(&u1, sizeof(u1),1, fp)!=0){
printf("error");
}
printf("%s", u1.name);
int amount = 0;
printf("Enter the amount to be withdrawn : ");
scanf("%d", &amount);
// fread(&u1, sizeof(u1), 1, fbin
if(u1.money<amount){
printf("Not enough money to withdraw.\n");
}
else{
u1.money = u1.money - amount;
printf("Money succesfully withdrawn.\n");
fseek(fp, 0, SEEK_SET);
fwrite(&u1, sizeof(u1),1, fp);
}
fclose(fp);
}
void main_menu(char username[]){
system("cls");
char username1[20];
strcpy(username1, username);
while(1){
printf("\t\t\t\tWELCOME %s\n", username);
printf("\t\t\t\t1.View account details\n");
printf("\t\t\t\t2.Update Account\n");
printf("\t\t\t\t3.Make a Transaction\n");
printf("\t\t\t\t4.To view all Transaction\n");
printf("\t\t\t\t5.Make a Deposit\n");
printf("\t\t\t\t6.Make a Withdrawl\n");
printf("\t\t\t\t7.LOG OUT\n");
int ch;
printf("Enter your choice : ");
scanf("%d", &ch);
if(ch==1){
view_acc(username1);
}
else if(ch==2){
update_acc(username1);
}
else if(ch==3){
transaction(username1);
}
else if(ch==4){
viewbalance(username1);
}
else if(ch==5){
deposit(username1);
}
else if(ch==6){
withdrawl(username1);
}
}
}
void update_acc(char username[]){
FILE *fp1, *fp2;
struct BMS u1;
struct BMS u2;
int ch, k=0;
char temp, ch1, filename[20];
strcpy(filename, username);
fp1 = fopen(strcat(filename, ".bin"), "rb");
fp2 = fopen("copy.bin", "ab");
if(fp1==NULL){
printf("error updating data!\n");
}
printf("Press 1 to update Name.\n");
printf("Press 2 to update Mobile number.\n");
printf("Press 3 to update Email id.\n");
printf("Press 4 to update Address.\n");
printf("Press 5 to update DOB.\n");
printf("Press 6 to update password.\n");
printf("Press 0 to go back\n");
printf("Enter your choice : ");
scanf("%d", &ch);
if(ch==1){
printf("Please enter your new name : ");
char name[20];
scanf("%d", &temp);
scanf("%[^\n]", &name);
while(fread(&u1, sizeof(u1), 1, fp1)){
if(strcmp(u1.name , name)){
fwrite(&u1, sizeof(u1), 1, fp2);
}
}
strcpy(u2.name , name);
fwrite(&u2, sizeof(u2), 1, fp2);
fclose(fp2);
fclose(fp1);
int del = remove(filename);
rename("copy.bin", filename);
printf("Name succesfully updated.\n");
}
else if(ch==2){
printf("Please enter your new mobile number : ");
unsigned long long contact;
scanf("%llu", &contact);
while(fread(&u1, sizeof(u1), 1, fp1)){
if(u1.contact != contact){
fwrite(&u1, sizeof(u1), 1, fp2);
}
}
u2.contact = contact;
fwrite(&u2, sizeof(u2), 1, fp2);
fclose(fp2);
fclose(fp1);
int del = remove(filename);
rename("copy.bin", filename);
printf("Contact succesfully updated.\n");
}
else if(ch==3){
printf("Please enter your new mail id : ");
scanf("%d", &temp);
char mailid[20];
scanf("%[^\n]", &mailid);
while(fread(&u1, sizeof(u1), 1, fp1)){
if(strcmp(u1.mailid , mailid)){
fwrite(&u1, sizeof(u1), 1, fp2);
}
}
strcpy(u2.mailid , mailid);
fwrite(&u2, sizeof(u2), 1, fp2);
fclose(fp2);
fclose(fp1);
int del = remove(filename);
rename("copy.bin", filename);
printf("Email succesfully updated.\n");
}
else if(ch==4){
printf("Please enter your new address : ");
scanf("%d", &temp);
char address[100];
scanf("%[^\n]", &address);
while(fread(&u1, sizeof(u1), 1, fp1)){
if(strcmp(u1.address , address)){
fwrite(&u1, sizeof(u1), 1, fp2);
}
}
strcpy(u2.address , address);
fwrite(&u2, sizeof(u2), 1, fp2);
fclose(fp2);
fclose(fp1);
int del = remove(filename);
rename("copy.bin", filename);
printf("Address succesfully updated.\n");
}
else if(ch==5){
printf("Enter your new DOB : ");
int date, month, year;
printf("Date : ");
scanf("%d" , &date);
printf("\nMonth : ");
scanf("%d", &month);
printf("\nYear : ");
scanf("%d", &year);
while(fread(&u1, sizeof(u1), 1, fp1)){
if(u1.date != date || u1.month != month || u1.year != year){
fwrite(&u1, sizeof(u1), 1, fp2);
}
}
u2.date = date;
u2.month = month;
u2.year = year;
fwrite(&u2, sizeof(u2), 1, fp2);
fclose(fp2);
fclose(fp1);
int del = remove(filename);
rename("copy.bin", filename);
printf("DOB succesfully updated.\n");
}
else if(ch==6){
printf("Enter your new PIN : ");
char password[15];
for(k=0; k<15; k++){
ch1 = getch();
password[k]=ch1;
if(ch1!=13 && ch1!=8)
printf("*");
if(ch1 == 13)
break;
}
password[k]='\0';
printf("\nYour new created PIN is : ");
for(k=0; k<strlen(password); k++){
printf("%c", password[k]);
}
while(fread(&u1, sizeof(u1), 1, fp1)){
if(strcmp(u1.password , password)){
fwrite(&u1, sizeof(u1), 1, fp2);
}
}
strcpy(u2.password , password);
fwrite(&u2, sizeof(u2), 1, fp2);
fclose(fp2);
fclose(fp1);
int del = remove(filename);
rename("copy.bin", filename);
printf("\nPassword succesfully updated.\n");
}
else if(ch==0){
fclose(fp1);
fclose(fp2);
}
}
// void deposit( char username[]){
// struct BMS u1;
// FILE *fp;
// char filename[20];
// strcpy(filename, username);
// fp = fopen(strcat(filename, ".txt"), "w+");
// fread(&u1, sizeof(u1),1, fp);
// int amount = 0;
// printf("Enter the amount to be deposited : ");
// scanf("%d", &amount);
// u1.money = u1.money + amount;
// printf("Money succesfully deposited.");
// fwrite(&u1, sizeof(u1),1, fp);
// fclose(fp);
// }
// void withdrawl( char username[]){
// struct BMS u1;
// FILE *fp;
// char filename[20];
// strcpy(filename, username);
// fp = fopen(strcat(filename, ".txt"), "w+");
// fread(&u1, sizeof(u1),1, fp);
// int amount = 0;
// printf("Enter the amount to be withdrawn : ");
// scanf("%d", &amount);
// fread(&u1, sizeof(u1), 1, fp);
// if(u1.money<amount){
// printf("Not enough money to withdraw.");
// }
// else{
// u1.money = u1.money - amount;
// printf("Money succesfully withdrawn.");
// fwrite(&u1, sizeof(u1),1, fp);
// }
// fclose(fp);
// }
int main()
{
system("cls");
struct BMS u1[N];
int choice;
int i = 0;
while (5)
{
printf("\t\t\t\tWELCOME TO THE BANK MANAGEMENT SYSTEM\n");
printf("\t\t\t\t1.Create new account\n");
printf("\t\t\t\t2.Login into existing account\n");
printf("\t\t\t\t6.Exit\n");
printf("\nEnter Your Choice : ");
scanf("%d", &choice);
if (choice == 1)
{
create_acc();
}
else if (choice == 2)
{
login();
}
else if (choice == 6){
}
}
//printf("Thanks For Coming !!");
return 0;
}
I'm doing my bank record management system project and I'm facing problem in reading from file. My file name is "name_of_the_user.txt" and opened in wb+ mode. I've a structure which have some fields such as name, address, money, etc. I've been trying to deposit and withdraw from account by reading the data into structure BMS u1 and then doing following functions, but I think reading into this structures leads to corruption of file and some garbage value. Please help.
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
void main_menu(char*);
void update_acc(char *);
// void deposit( char *);
// void withdrawl( char *);
struct BMS
{
char name[20];
char mailid[20];
unsigned long long contact;
unsigned long long adhar;
int date, year, month;
char address[100];
char password[15];
int money;
};
struct balance{
char namefrom[20];
char nameto[20];
long int money;
};
void gotoxy(int x, int y)
{
COORD c;
c.X = x;
c.Y = y;
SetConsoleCursorPosition(
GetStdHandle(STD_OUTPUT_HANDLE), c);
}
void view_acc(char username[])
{
FILE *fp;
struct BMS u1;
char filename[20];
strcpy(filename, username);
fp = fopen(strcat(filename, ".bin"), "rb");
if(fp==NULL){
printf("Error viewing account details.");
}
fread(&u1, sizeof(u1), 1, fp);
printf("%s\n", u1.name);
printf("%llu\n", u1.contact);
printf("%llu\n", u1.adhar);
printf("%d - %d - %d\n", u1.date, u1.month, u1.year);
printf("%s\n", u1.address);
printf("%s\n", u1.mailid);
printf("Balance : %d\n", u1.money);
fclose(fp);
}
void create_acc()
{
system("cls");
FILE *fp;
struct BMS u1;
int k=0;
char ch1, temp, filename[20];
scanf("%c",&temp);
u1.money = 0;
printf("\t\t\tFill in the details \n");
printf("Name of account holder: ");
scanf("%[^\n]", &u1.name);
printf("Enter DOB : ");
printf("Date : ");
scanf("%d" , &u1.date);
printf("\nMonth : ");
scanf("%d", &u1.month);
printf("\nYear : ");
scanf("%d", &u1.year);
printf("Enter your mobile number : ");
scanf("%llu", &u1.contact);
printf("Enter your Aadhar ID : ");
scanf("%llu", &u1.adhar);
scanf("%c",&temp);
printf("Enter user email : ");
scanf("%[^\n]", &u1.mailid);
scanf("%c",&temp);
printf("Enter Your Address : ");
scanf("%[^\n]", &u1.address);
printf("Create PIN : ");
for(k=0; k<15; k++)
{
ch1 = getch();
u1.password[k]=ch1;
if(ch1!=13 && ch1!=8)
printf("*");
if(ch1 == 13)
break;
}
u1.password[k]='\0';
printf("\nYour created PIN is : ");
for(k=0; k<strlen(u1.password); k++){
printf("%c", u1.password[k]);
}
printf("\nYour Account has beem succesfully added to our system. Please remember your created PIN to acces your account.\n\n");
strcpy(filename, u1.name);
fp = fopen(strcat(filename, ".bin"), "ab");
fwrite(&u1, sizeof(u1),1, fp);
fclose(fp);
}
void login(){
system("cls");
FILE *fp;
struct BMS u1;
char name[20], temp, password[15],ch1, filename[20];
int i, j, k;
scanf("%c",&temp);
printf("Enter your name : ");
scanf("%[^\n]", &name);
scanf("%c",&temp);
printf("Enter your PIN : ");
scanf("%s", &password);
strcpy(filename, name);
fp = fopen(strcat(filename, ".bin"),"rb");
if(fp==NULL){
printf("No account found.");
}
fread(&u1, sizeof(u1), 1, fp);
if(!strcmp(password, u1.password)){
printf("Login succesful.");
fclose(fp);
main_menu(name);
}
else {
printf("Login failed wrong password.");
fclose(fp);
}
}
void transaction(char username[])
{
system("cls");
FILE *fp, *fp1, *fm;
struct BMS u1;
struct balance b1;
char userfrom[20], userto[20],username1, username2, temp;
// strcpy(username1, username);
scanf("%c", &temp);
printf("Transfer money to : ");
scanf("%[^\n]", &userto);
printf("Enter the amount to be transferred : ");
scanf("%ld", &b1.money);
// fp1 = fopen(strcat(username1, ".txt"), )
strcpy(userfrom, username);
strcpy(username, userto);
fp = fopen(strcat(username, ".bin"), "rb");
fm = fopen("money.bin", "ab");
while (fread(&u1, sizeof(u1), 1, fp))
{
if (strcmp(userto, u1.name) == 0) {
strcpy(b1.nameto, u1.name);
strcpy(b1.namefrom, userfrom);
}
}
fwrite(&b1, sizeof(b1), 1, fm);
fclose(fm);
fclose(fp);
}
void viewbalance(char username[]){
system("cls");
FILE *fm;
struct balance b1;
char ch;
int i=1 , money=0;
fm = fopen("money.bin", "rb");
int k = 5, l = 10;
int m = 30, n = 10;
int u = 60, v = 10;
gotoxy(30, 2);
printf("==== BALANCE DASHBOARD ====");
gotoxy(30, 3);
printf("***************************");
gotoxy(k, l);
printf("S NO.");
gotoxy(m, n);
printf("TRANSACTION ID");
gotoxy(u, v);
printf("AMOUNT");
while (fread(&b1, sizeof(b1), 1, fm)) {
if (strcmp(username, b1.nameto) == 0) {
gotoxy(k, ++l);
printf("%d", i);
i++;
gotoxy(m, ++n);
printf("%s", b1.namefrom);
gotoxy(u, ++v);
printf("%ld", b1.money);
money = money + b1.money;
}
}
gotoxy(80, 10);
printf("TOTAL AMOUNT");
gotoxy(80, 12);
printf("%d", money);
getch();
fclose(fm);
}
void deposit( char username[]){
struct BMS u1;
FILE *fp;
char filename[20];
strcpy(filename, username);
fp = fopen(strcat(filename, ".bin"), "wb+");
fread(&u1, sizeof(u1),1, fp);
int amount = 0;
printf("Enter the amount to be deposited : ");
scanf("%d", &amount);
u1.money = u1.money + amount;
printf("Money succesfully deposited.");
fseek(fp, 0, SEEK_SET);
fwrite(&u1, sizeof(u1),1, fp);
fclose(fp);
}
void withdrawl( char username[]){
struct BMS u1;
FILE *fp;
char filename[20];
strcpy(filename, username);
fp = fopen(strcat(filename, ".bin"), "wb+");
if(fp==NULL){
printf("Error");
}
printf("%s", u1.name);
if(fread(&u1, sizeof(u1),1, fp)!=0){
printf("error");
}
printf("%s", u1.name);
int amount = 0;
printf("Enter the amount to be withdrawn : ");
scanf("%d", &amount);
// fread(&u1, sizeof(u1), 1, fbin
if(u1.money<amount){
printf("Not enough money to withdraw.\n");
}
else{
u1.money = u1.money - amount;
printf("Money succesfully withdrawn.\n");
fseek(fp, 0, SEEK_SET);
fwrite(&u1, sizeof(u1),1, fp);
}
fclose(fp);
}
void main_menu(char username[]){
system("cls");
char username1[20];
strcpy(username1, username);
while(1){
printf("\t\t\t\tWELCOME %s\n", username);
printf("\t\t\t\t1.View account details\n");
printf("\t\t\t\t2.Update Account\n");
printf("\t\t\t\t3.Make a Transaction\n");
printf("\t\t\t\t4.To view all Transaction\n");
printf("\t\t\t\t5.Make a Deposit\n");
printf("\t\t\t\t6.Make a Withdrawl\n");
printf("\t\t\t\t7.LOG OUT\n");
int ch;
printf("Enter your choice : ");
scanf("%d", &ch);
if(ch==1){
view_acc(username1);
}
else if(ch==2){
update_acc(username1);
}
else if(ch==3){
transaction(username1);
}
else if(ch==4){
viewbalance(username1);
}
else if(ch==5){
deposit(username1);
}
else if(ch==6){
withdrawl(username1);
}
}
}
void update_acc(char username[]){
FILE *fp1, *fp2;
struct BMS u1;
struct BMS u2;
int ch, k=0;
char temp, ch1, filename[20];
strcpy(filename, username);
fp1 = fopen(strcat(filename, ".bin"), "rb");
fp2 = fopen("copy.bin", "ab");
if(fp1==NULL){
printf("error updating data!\n");
}
printf("Press 1 to update Name.\n");
printf("Press 2 to update Mobile number.\n");
printf("Press 3 to update Email id.\n");
printf("Press 4 to update Address.\n");
printf("Press 5 to update DOB.\n");
printf("Press 6 to update password.\n");
printf("Press 0 to go back\n");
printf("Enter your choice : ");
scanf("%d", &ch);
if(ch==1){
printf("Please enter your new name : ");
char name[20];
scanf("%d", &temp);
scanf("%[^\n]", &name);
while(fread(&u1, sizeof(u1), 1, fp1)){
if(strcmp(u1.name , name)){
fwrite(&u1, sizeof(u1), 1, fp2);
}
}
strcpy(u2.name , name);
fwrite(&u2, sizeof(u2), 1, fp2);
fclose(fp2);
fclose(fp1);
int del = remove(filename);
rename("copy.bin", filename);
printf("Name succesfully updated.\n");
}
else if(ch==2){
printf("Please enter your new mobile number : ");
unsigned long long contact;
scanf("%llu", &contact);
while(fread(&u1, sizeof(u1), 1, fp1)){
if(u1.contact != contact){
fwrite(&u1, sizeof(u1), 1, fp2);
}
}
u2.contact = contact;
fwrite(&u2, sizeof(u2), 1, fp2);
fclose(fp2);
fclose(fp1);
int del = remove(filename);
rename("copy.bin", filename);
printf("Contact succesfully updated.\n");
}
else if(ch==3){
printf("Please enter your new mail id : ");
scanf("%d", &temp);
char mailid[20];
scanf("%[^\n]", &mailid);
while(fread(&u1, sizeof(u1), 1, fp1)){
if(strcmp(u1.mailid , mailid)){
fwrite(&u1, sizeof(u1), 1, fp2);
}
}
strcpy(u2.mailid , mailid);
fwrite(&u2, sizeof(u2), 1, fp2);
fclose(fp2);
fclose(fp1);
int del = remove(filename);
rename("copy.bin", filename);
printf("Email succesfully updated.\n");
}
else if(ch==4){
printf("Please enter your new address : ");
scanf("%d", &temp);
char address[100];
scanf("%[^\n]", &address);
while(fread(&u1, sizeof(u1), 1, fp1)){
if(strcmp(u1.address , address)){
fwrite(&u1, sizeof(u1), 1, fp2);
}
}
strcpy(u2.address , address);
fwrite(&u2, sizeof(u2), 1, fp2);
fclose(fp2);
fclose(fp1);
int del = remove(filename);
rename("copy.bin", filename);
printf("Address succesfully updated.\n");
}
else if(ch==5){
printf("Enter your new DOB : ");
int date, month, year;
printf("Date : ");
scanf("%d" , &date);
printf("\nMonth : ");
scanf("%d", &month);
printf("\nYear : ");
scanf("%d", &year);
while(fread(&u1, sizeof(u1), 1, fp1)){
if(u1.date != date || u1.month != month || u1.year != year){
fwrite(&u1, sizeof(u1), 1, fp2);
}
}
u2.date = date;
u2.month = month;
u2.year = year;
fwrite(&u2, sizeof(u2), 1, fp2);
fclose(fp2);
fclose(fp1);
int del = remove(filename);
rename("copy.bin", filename);
printf("DOB succesfully updated.\n");
}
else if(ch==6){
printf("Enter your new PIN : ");
char password[15];
for(k=0; k<15; k++){
ch1 = getch();
password[k]=ch1;
if(ch1!=13 && ch1!=8)
printf("*");
if(ch1 == 13)
break;
}
password[k]='\0';
printf("\nYour new created PIN is : ");
for(k=0; k<strlen(password); k++){
printf("%c", password[k]);
}
while(fread(&u1, sizeof(u1), 1, fp1)){
if(strcmp(u1.password , password)){
fwrite(&u1, sizeof(u1), 1, fp2);
}
}
strcpy(u2.password , password);
fwrite(&u2, sizeof(u2), 1, fp2);
fclose(fp2);
fclose(fp1);
int del = remove(filename);
rename("copy.bin", filename);
printf("\nPassword succesfully updated.\n");
}
else if(ch==0){
fclose(fp1);
fclose(fp2);
}
}
// void deposit( char username[]){
// struct BMS u1;
// FILE *fp;
// char filename[20];
// strcpy(filename, username);
// fp = fopen(strcat(filename, ".txt"), "w+");
// fread(&u1, sizeof(u1),1, fp);
// int amount = 0;
// printf("Enter the amount to be deposited : ");
// scanf("%d", &amount);
// u1.money = u1.money + amount;
// printf("Money succesfully deposited.");
// fwrite(&u1, sizeof(u1),1, fp);
// fclose(fp);
// }
// void withdrawl( char username[]){
// struct BMS u1;
// FILE *fp;
// char filename[20];
// strcpy(filename, username);
// fp = fopen(strcat(filename, ".txt"), "w+");
// fread(&u1, sizeof(u1),1, fp);
// int amount = 0;
// printf("Enter the amount to be withdrawn : ");
// scanf("%d", &amount);
// fread(&u1, sizeof(u1), 1, fp);
// if(u1.money<amount){
// printf("Not enough money to withdraw.");
// }
// else{
// u1.money = u1.money - amount;
// printf("Money succesfully withdrawn.");
// fwrite(&u1, sizeof(u1),1, fp);
// }
// fclose(fp);
// }
int main()
{
system("cls");
struct BMS u1[N];
int choice;
int i = 0;
while (5)
{
printf("\t\t\t\tWELCOME TO THE BANK MANAGEMENT SYSTEM\n");
printf("\t\t\t\t1.Create new account\n");
printf("\t\t\t\t2.Login into existing account\n");
printf("\t\t\t\t6.Exit\n");
printf("\nEnter Your Choice : ");
scanf("%d", &choice);
if (choice == 1)
{
create_acc();
}
else if (choice == 2)
{
login();
}
else if (choice == 6){
}
}
//printf("Thanks For Coming !!");
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在写入之前,您需要查找文件的开头。否则,它将在您停止读取的位置写入更新的记录,而不是覆盖原始记录。
You need to seek to the beginning of the file before you write. Otherwise it writes the updated record at the place where you left off reading, instead of overwriting the original record.