C语言银行客户管理程序无法读取指定.txt客户文件问题
银行客户管理程序客户列表读取失败问题
我开发的银行客户管理程序中,添加客户时会生成以「FirstName_LastName.txt」命名的客户信息文件,但选择“查看客户列表”功能时,程序本该读取文件并显示客户姓名,却始终提示“未找到客户”(count值为0)。即便文件存放路径正确,移除count判断逻辑后问题仍未解决。
struct Info { char FirstName[20]; char LastName[20]; int AccNumber; float Balance; }; int main() { int Selection; printf("\n*******************************\n"); printf("\nWelcome to World Bank!\n"); printf("\nPlease Enter a Selection Below!\n"); printf("\n1: View Customers\n"); printf("2: View Total Assets\n"); printf("3: Administrator View\n"); printf("4: Exit\n"); printf("\n*******************************\n"); scanf("%d", &Selection); while (getchar() != '\n'); if(Selection == 1){ system("cls"); printf("\nCustomer List\n"); printf("*******************************\n"); char filename[40]; struct Info customer; int count = 0; for (char c = 'A'; c <= 'Z'; c++){ for (char d = 'A'; d <= 'Z'; d++){ sprintf(filename, "%c%c.txt", c, d); FILE *Fpointer = fopen(filename, "r"); if(Fpointer != NULL){ fscanf(Fpointer, "Name: %s %s\nAccount number: %d\nBalance: %f", customer.FirstName, customer.LastName, &customer.AccNumber, &customer.Balance); printf("%s %s\n", customer.FirstName, customer.LastName); fclose(Fpointer); count++; } } } if (count == 0){ printf("No Customers Found.\n"); } getchar(); } if (Selection == 2){ } if (Selection == 3){ int Selection2; system("cls"); FILE * Fpointer; printf("Administrator View\n"); printf("\n*******************************\n"); printf("\nPlease Make a Selection:\n"); printf("1: Add New Customer\n"); printf("2: Edit Current Customer\n"); printf("3: Remove Customer\n"); scanf("%d", &Selection2); while (getchar() != '\n'); if (Selection2 == 1){ char filename[40]; struct Info new_customer; printf("Enter Customer First Name: "); scanf("%s", new_customer.FirstName); while (getchar() != '\n'); printf("Enter Customer Last Name: "); scanf("%s", new_customer.LastName); while (getchar() != '\n'); printf("Enter Account Number: "); scanf("%d", &new_customer.AccNumber); while (getchar() != '\n'); printf("Enter Balance (No Commas): $"); scanf("%f", &new_customer.Balance); while (getchar() != '\n'); sprintf(filename, "%s_%s.txt", new_customer.FirstName, new_customer.LastName); Fpointer = fopen(filename , "w"); if (Fpointer == NULL){ printf("Error: Could not create file.\n"); return 1; } fprintf(Fpointer, "Name: %s %s\nAccount number: %d\nBalance: %.2f", new_customer.FirstName, new_customer.LastName, new_customer.AccNumber, new_customer.Balance); fclose(Fpointer); } } else if (Selection == 4){ system("cls"); printf("\n*******************************\n"); printf("Thank you for Banking with us Today!\n"); printf("Press any Key to Exit"); printf("\n*******************************\n"); getchar(); return 0; } return 0; }
问题根源
查看客户列表的代码里,循环生成的文件名格式是%c%c.txt,也就是两个大写字母组成的文件名(比如AA.txt、AB.txt),但添加客户时生成的文件名格式是%s_%s.txt(比如John_Doe.txt),两者完全不匹配,导致程序找不到正确的客户文件,自然count始终为0。
修复步骤
- 修改查看客户列表的文件名生成逻辑,不再遍历字母组合,改为扫描目录下所有符合
*_*.txt格式的文件,或者维护一个客户索引文件记录所有已创建的客户信息。 - 下面是Windows平台下使用目录遍历的修复示例:
#include <windows.h> // 需要添加Windows头文件 // 在Selection == 1的分支中替换原循环逻辑 WIN32_FIND_DATA findData; HANDLE hFind = FindFirstFile("*_*.txt", &findData); if (hFind != INVALID_HANDLE_VALUE) { do { // 跳过目录类型的条目 if (!(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { FILE *Fpointer = fopen(findData.cFileName, "r"); if (Fpointer != NULL) { fscanf(Fpointer, "Name: %s %s\nAccount number: %d\nBalance: %f", customer.FirstName, customer.LastName, &customer.AccNumber, &customer.Balance); printf("%s %s\n", customer.FirstName, customer.LastName); fclose(Fpointer); count++; } } } while (FindNextFile(hFind, &findData)); FindClose(hFind); }
其他优化方案
- 若需要跨平台运行,可使用标准C库的目录遍历方法(Linux下用
opendir/readdir)。 - 更高效的方式是在添加客户时,把客户姓名或文件名写入一个统一的索引文件(比如
customers_index.txt),查看客户列表时直接读取这个索引文件,再逐个打开对应的客户信息文件,这种方法更易维护且性能更好。
内容的提问来源于stack exchange,提问作者TJP
相关产品推荐
相关产品推荐

