void transfer(void){
char deb[11], cre[11], tim[11];
int amount;
time_t t;
struct tm *p;
FILE *old, *new, *record;
struct Account *account=(struct Account *)malloc(sizeof(struct Account));
struct Account *debit=(struct Account *)malloc(sizeof(struct Account));
struct Account *credit=(struct Account *)malloc(sizeof(struct Account));
struct Transaction *transaction=(struct Transaction *)malloc(sizeof(struct Transaction));
if((old=fopen("Account.txt", "r"))==NULL){
printf("Fail to open the file!\n");
exit(EXIT_FAILURE);
}
if((new=fopen("Modify.txt", "w"))==NULL){
printf("Fail to open the file!\n");
exit(EXIT_FAILURE);
}
if((record=fopen("Transaction.txt", "a"))==NULL){
printf("Fail to open the file!\n");
exit(EXIT_FAILURE);
}
printf("Enter the debiting account number: ");
scanf("%s", deb);
getchar();
printf("\n");
while((fread(debit, sizeof(struct Account), 1, old))!=0){
if(strcmp(debit->accountnumber, deb)==0){
printf("Name of the account holder: %s\n", debit->name);
printf("Id number of the account holder: %s\n", debit->id);
printf("Balance of the account: %d\n\n", debit->balance);
break;
}
}
printf("Enter the crediting account number: ");
scanf("%s", cre);
getchar();
printf("\n");
fseek(old, 0, SEEK_SET);
while((fread(credit, sizeof(struct Account), 1, old))!=0){
if(strcmp(credit->accountnumber, cre)==0){
printf("Name of the account holder: %s\n", credit->name);
printf("Id number of the account holder: %s\n", credit->id);
printf("Balance of the account: %d\n\n", credit->balance);
break;
}
}
amount=(debit->balance)+1;
while(amount>(debit->balance)){
printf("Enter the transfer amount: ");
scanf("%d", &amount);
getchar();
if(amount>(debit->balance)){
printf("\nTransfer amount can't be more than the balance of the debiting account.\n\n");
}
}
printf("\nTransaction successful.\n\n");
(debit->balance)-=amount;
(credit->balance)+=amount;
printf("Balance of the account %s is: %d\n", debit->accountnumber, debit->balance);
printf("Balance of the account %s is: %d\n\n", credit->accountnumber, credit->balance);
fseek(old, 0, SEEK_SET);
while((fread(account, sizeof(struct Account), 1, old))!=0){
if((account->accountnumber)==(debit->accountnumber)){
account=debit;
}
else if((account->accountnumber)==(credit->accountnumber)){
account=credit;
}
fwrite(account, sizeof(struct Account), 1, new);
}
fclose(old);
fclose(new);
if((old=fopen("Account.txt", "w"))==NULL){
printf("Fail to open the file!\n");
exit(EXIT_FAILURE);
}
if((new=fopen("Modify.txt", "r"))==NULL){
printf("Fail to open the file!\n");
exit(EXIT_FAILURE);
}
while((fread(account, sizeof(struct Account), 1, new))!=0){
fwrite(account, sizeof(struct Account), 1, old);
}
fclose(old);
fclose(new);
free(account);
time(&t);
p=localtime(&t);
sprintf(transaction->Dot, "%d/%02d/%02d",1900+(p->tm_year), 1+(p->tm_mon), p->tm_mday);
transaction->Dot[10]='\0';
strcpy(transaction->debitac, debit->accountnumber);
strcpy(transaction->creditac, credit->accountnumber);
transaction->figure=amount;
fwrite(transaction, sizeof(struct Transaction), 1, record);
fclose(record);
free(debit);
free(credit);
free(transaction);
printf("\n");
}
我写的这个功能,在空白的那几行之后的代码都没有运行,不知道是为什么,应该怎么改?
等于说虽然成功得到了debit和credit两个结构体,也成功修改了,但是存不回去。