所以我们试着用C语言做一个简单的银行程序。有一个名为AccountInfo.txt的文本文件,其中包含帐户持有人姓名,帐号,帐户类型,nid,电话号码等。该程序的特点之一是一个单一的用户可以有多个银行帐户。因此,我尝试通过一个名为SearchAndFind的用户定义函数来实现该特性,该函数将搜索文本文件并打印与用户匹配的所有帐号提供的用户名。下面是代码:(请注意,代码仍然是不完整的,效率也很低。请忽略这一点)
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#define USER_PASS "./username.txt"
#define ACCOUNT_DATA "./AccountInfo.txt"
typedef struct
{
char Name[30];
char AccountType[20];
long long AccountNo;
long long Balance;
long long Phone;
long long NID;
char Username[30];
} AccountInfo;
int loginverify(char *userid, char *pass);
void createaccount();
void mainmenu(char *usernm);
int ReadAccountInfo();
void ViewAccounts(char *usrnm, int sumaccounts);
void SearchAndPrint(char *username);
int main()
{
int flag, signinoption;
char user_name[20], password[30];
printf("Welcome to My Bank\n");
while (1)
{
printf("1. Login\n");
printf("2. Create a new account\n");
printf("3. Exit\n");
printf("Choose your option: ");
scanf("%d", &signinoption);
printf("\n");
switch (signinoption)
{
case 1:
while (1)
{
printf("Enter Username: ");
scanf("%19s", user_name);
printf("Enter Password: ");
scanf("%29s", password);
flag = loginverify(user_name, password);
if (flag == 1)
{
printf("\nLogin successful. \n");
printf("\n");
mainmenu(user_name);
break;
}
else
{
printf("Login Failed. Try again. \n");
printf("\n");
}
}
break;
case 2:
createaccount();
break;
case 3:
return 0;
break;
}
if (signinoption == 1)
break;
}
return 0;
}
void mainmenu(char *usernm)
{
int menuchoice = 100;
char usrnm[30];
strcpy(usrnm, usernm);
while (menuchoice != 12)
{
printf("1. View All Accounts\n");
printf("2. Check Balance\n");
printf("3. Cash Deposit\n");
printf("4. Cash Withdraw\n");
printf("5. Fund Transfer\n");
printf("6. Benificiary Management\n");
printf("7. Mini Statement\n");
printf("8. Account Settings\n");
printf("9. Close Account\n");
printf("10. ATM/Branch Locations\n");
printf("11. Customer Support\n");
printf("12. Exit\n");
printf("\n");
printf("Choose your option: ");
scanf("%d", &menuchoice);
printf("\n");
if (menuchoice == 1)
SearchAndPrint(usrnm);
}
}
int loginverify(char *userid, char *pass)
{
FILE *fp;
char user[20], password[30];
int found = 0;
fp = fopen(USER_PASS, "r");
if (fp == NULL)
{
printf("Error: Could not open file\n");
return 0;
}
while (fscanf(fp, "Username: %s\nPassword: %s\n", user, password) == 2)
{
if (strcmp(userid, user) == 0 && strcmp(pass, password) == 0)
{
found = 1;
break;
}
}
fclose(fp);
return found;
}
void createaccount()
{
char username[20], password[20];
printf("Enter your login credentials: \n");
// a function can be added to check uniquenness
printf("Enter Username: ");
scanf("%s", username);
printf("Enter Password: ");
scanf("%s", password);
printf("\n");
printf("Enter your Account details: \n");
printf("Enter your Name: ");
printf("Enter your Account Type (Savings/Current/Fixed Deposit): ");
// here unique account number is requires. or it can be generated automatically
printf("Enter your Account No: ");
printf("Enter your Balance: ");
printf("Enter your Phone No: ");
printf("Enter your NID No: ");
printf("\n");
printf("Account Created Successfully\n");
printf("\n");
}
void SearchAndPrint(char *username)
{
printf("\n%s\n", username);
FILE *fp;
AccountInfo acc;
int found = 0;
fp = fopen(ACCOUNT_DATA, "r");
if (fp == NULL)
{
printf("Error: Could not open file\n");
return;
}
while (fscanf(fp, "Name: %s\nAccount Type: %s\nAccount No: %lld\nBalance: %lld\nPhone: %lld\nNID No: %lld\nUsername: %s\n", acc.Name, acc.AccountType, &acc.AccountNo, &acc.Balance, &acc.Phone, &acc.NID, acc.Username) == 7)
{
printf("Name: %s\nAccount Type: %s\nAccount No: %lld\nBalance: %lld\nPhone: %lld\nNID No: %lld\nUsername: %s\n", acc.Name, acc.AccountType, acc.AccountNo, acc.Balance, acc.Phone, acc.NID, acc.Username);
if (strcmp(username, acc.Username) == 0)
{
found = 1;
printf("Account No: %lld\nAccount Type: %s\n", acc.AccountNo, acc.AccountType);
}
}
fclose(fp);
if (!found)
{
printf("No matching accounts found\n");
}
}
现在从主菜单中输入1时,它会转到SearchAndPrint函数,然后执行以下行:printf(“\n%s\n”,用户名);但是之后什么都没有。我试过几种方法,但是都不起作用。我假设阅读取文件的内容一定有问题。
以下是AccountInfo.txt文件包含的内容:https://pastebin.com/y2qypf3s
对于登录凭据,您可以使用以下任一项:https://pastebin.com/iF1BTuZB
我尝试了while循环的不同变体来从文件中获取输入。我希望打印出与所提供的用户名匹配的所有条目的帐户信息。
1条答案
按热度按时间uqjltbpv1#
在试用您的程序时,我也无法列出任何帐户信息,因为帐户名实际上是由带有嵌入空格的字符串组成的。在前面的代码中使用过“scanf”函数和带有嵌入空格的字符串之后,我确实尝试了一些使用格式化文字“%[^\n]”的重构代码,正如上面的好评所指出的。然而,为了使格式化文字工作也需要对帐户信息文件进行调整。
首先,下面是对“fscanf”函数进行了改进的程序重构版本。
需要注意的一些关键点。
经过这些调整后,下面是针对一个帐户测试程序的终端输出。
给予这些调整,看看它是否符合您项目的精神。