我的数据库程序没有打开或读取包含数据的 .txt 文件

问题描述 投票:0回答:1

创建了一个包含客户信息的 .txt 文件(标题为 FIRSTname_LASTname.txt,具体取决于他们的名字),但是当我选择查看客户列表时,它应该读取 .txt 文件的名称(他们的名字)并显示它,但事实并非如此。由于某种原因,count == 0 所以即使 .txt 文件位于正确的位置,它也只是说“找不到客户”。

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;
}

我尝试删除 if(count == 0) 但即使没有该参数它仍然无法正常工作,我还尝试将 .txt 文件放在每个可能需要读取它的地方。

c file text
1个回答
0
投票

你写入的路径:

sprintf(filename, "%s_%s.txt", new_customer.FirstName, new_customer.LastName);

和你读到的路径不一样:

                sprintf(filename, "%c%c.txt", c, d);

总是检查返回值,否则你就是在浪费时间。这个缺陷告诉您您想要消除代码重复。除非您有其他要求,否则我建议您使用单个文件,例如 customers.txt。或者,您需要查看当前目录以找到合适的文件,或者以众所周知的名称维护现有客户文件的索引。在索引中查找客户,然后打开它告诉你的文件。

system("cls")
是 Windows 终端特定的;考虑改用 ANSI 序列。

为您的菜单选择使用枚举和/或常量,这样您就可以:

    if(Selection == CUSTOMER){

代替:

    if(Selection == 1){

如果您输入 EOF(在 Linux 上为 ctrl-D),这将触发无限循环:

while (getchar() != '\n');

你想做的事:

for(;;) {
   int ch = getchar();
   if(ch == EOF || ch == '\n') break;
}

我建议您为此创建一个函数,或者更好的是,切换到使用

fgets()
读取一行输入,然后使用
sscanf()
从中提取您需要的内容。忽略意外输入。

© www.soinside.com 2019 - 2024. All rights reserved.