我正在尝试制作一个简单的银行程序,并试图让存款部分正常工作,我正在使用一个文本文件来存储信息,例如帐号、账户持有人的名字和姓氏以及余额在帐户中。
我在这里尝试做的是让用户输入一个帐号,然后程序将打开名为 Bank.txt 的现有文件并检查是否有匹配的帐号。如果有,程序将继续要求用户输入要存入的金额,如果没有,程序将简单地告诉用户该帐号不存在。
我遇到的问题是,当我第一次运行该程序并创建一个帐户时,一切都运行良好,但是一旦我关闭该程序并重新打开它,即使它已经存在,它也没有在文件中找到匹配的帐号.
完整代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<conio.h>
#define SIZE 10 000
#define NAME_LEN 60
#define MAX_ACCOUNT 1000
struct bank
{
int account_number;
char first_name[NAME_LEN+1];
char last_name[NAME_LEN+1];
float accountBalance;
} enter;
void create();
/*void searchAccounts();
void viewAccounts();
void sortAccounts();
void withdraw();
void deposit();
void Account_Balance();
void Account_eliminate();
*/
int main(void)
{
int ch;
while(1)
{
printf("\t\t\t Welcome to KTH Elektro Bank! \n \t\t Please select your options form the menu below 1-9\n\n");
printf("\t\t 1 : Create Account\n");
printf("\t\t 2 : Search Accounts\n");
printf("\t\t 3 : View Accounts\n");
printf("\t\t 4 : Sort Accounts\n");
printf("\t\t 5 : Deposit \n");
printf("\t\t 6 : Withdraw \n");
printf("\t\t 7 : Check Balance \n");
printf("\t\t 8 : Delete Account \n");
printf("\t\t 9 : Exit \n\n");
printf("\t\t please enter choice : ");
scanf("%d", &ch);
switch(ch)
{
case 1: create();
break;
/* case 2: searchAccounts();
break;
case 3: viewAccounts();
break;
case 4: sortAccounts();
break;*/
case 5: deposit();
break;/*
case 6: withdraw();
break;
case 7: Account_Balance();
break;
case 8: Account_eliminate();
break;*/
case 9: exit(0);
default: printf("\t\tEnter 1-9 Only");
getch();
}
}
}
void create()
{
FILE *originalFile = fopen("Bank.txt","w");
printf("\n\t\t Account Creation Page\n");
printf("\t\t Enter Desired Account Number Minimum 6 Digits! : ");
scanf("%d", &enter.account_number);
printf("\t\t Enter Your First Name : ");
scanf("%s", enter.first_name);
printf("\t\t Enter Your Last Name : ");
scanf("%s", enter.last_name);
printf("\t\t Your balance is by default set to 0 KR \n\n");
enter.accountBalance = 0;
fprintf(originalFile,"\n Account Number : %d \n First Name : %s \n Last Name : %s \n Balance : %f", enter.account_number,enter.first_name, enter.last_name, enter.accountBalance);
fclose(originalFile);
getch();
}
void deposit()
{
printf("\n\t\t Deposit Page\n");
int acc_no;
printf("\t\t Enter The Account Number of the Account \n\t\t You Would Like To Deposit To : ");
scanf("%d", &acc_no);
FILE *originalFile = fopen("Bank.txt", "r");
FILE *newFile = fopen("BankTemp.txt", "w");
fscanf(originalFile, "Account Number : %d", enter.account_number);
fscanf(originalFile, "Account Holder First Name : %s", enter.first_name);
fscanf(originalFile, "Account Holder Last Nam : %s", enter.last_name);
fscanf(originalFile, "Balance : %f", enter.accountBalance);
if (acc_no == enter.account_number)
{
float balance;
printf("\n\t\t Enter The Amount You Would Like To Deposit : ");
scanf("%f", &balance);
enter.accountBalance = balance + enter.accountBalance;
fprintf(newFile, "\n Account Number : %d \n First Name : %s \n Last Name : %s \n Balance : %f", enter.account_number, enter.first_name, enter.last_name, enter.accountBalance);
fclose(newFile);
fclose(originalFile);
remove("Bank.txt");
rename("BankTemp.txt", "Bank.txt");
}
if(acc_no != enter.account_number)
{
printf("\t\t Account Number Doesn't Exist\n");
fclose(originalFile);
fclose(newFile);
remove("BankTemp.txt");
}
}
最佳答案
来自 man scanf
,但 fscanf()
的工作方式类似,关于在格式字符串中指定字符时要读取的内容:
An ordinary character (i.e., one other than white space or '%'). This character must exactly match the next character of input.
例如,当您这样做时:
fscanf(originalFile, "Account Holder First Name : %s", enter.first_name);
文件中没有这个确切的文本,您写了类似 "First Name : %s\n"
的内容。那将不匹配。
此外,如果用户名中有一些空格,代码将无法按预期工作。我的建议是简化格式,也许通过在每一行上写一个数据。然后使用 fgets()
读取整行,并在需要时使用 scanf()
解析该行。
在更严肃的项目中,您可能需要考虑使用合适的数据库,甚至是 sqlite。
关于c - 使用 C 中的文本文件的银行帐户存款选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33133262/