无法读取C中的.dat文件

无法读取C中的.dat文件,c,file-handling,C,File Handling,我创建了一个程序,该程序考虑了来自用户的信息,并将其存储到accounts.dat文件中。由于该程序目前运行正常,似乎一切正常,直到我关闭它并重新打开进行测试,它不会读取我已经输入的任何信息。案例6是从accounts.dat读取数据的地方。每次我使用accounts.dat时,它似乎都充满了信息 /* HEADER FILES */ #include <stdio.h> #include <string.h> #include <stdlib.h> /*

我创建了一个程序,该程序考虑了来自用户的信息,并将其存储到accounts.dat文件中。由于该程序目前运行正常,似乎一切正常,直到我关闭它并重新打开进行测试,它不会读取我已经输入的任何信息。案例6是从accounts.dat读取数据的地方。每次我使用accounts.dat时,它似乎都充满了信息

/* HEADER FILES */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

/* STRUCT TO HOLD ACCOUNT INFO */
struct bank
{
    char first[25];
    char middle[25];
    char last[25];
    long int aNumber;
    float aBalance;
};

/* MAIN START*/
int main()
{
    int choice; /* SWITCH-STATEMENT CASE */
    int i; /* FOR-LOOP COUNTER */
    int  count; /* KEEPS TRACK OF TOTAL # OF ACCOUNTS */
    long int aNumber; /* TEMPORARILY HOLDS ACCOUNT # FOR SEARCH IN FILE */
    float amount; /* TEMPORARILY HOLDS A DOLLAR AMOUNT FOR VARIOUS REASONS */
    FILE *fp; /* SPECIFIC FILE DECLARATION FOR OUR BANK */
    struct bank account[50]; /* ARRAY DECLARATION FOR STRUCT */

    /* DO/WHILE START */
    do
    {
        /* OPTION MENU */
        printf("\n\nWelcome to yor bank access.\n"
               "Please follow the prompts below.\n\n"
               "\t0. Exit\n"
               "\t1. Deposit\n"
               "\t2. Withdrawal\n"
               "\t3. Add account\n"
               "\t4. Remove Account\n"
               "\t5. Balance Inquiry\n"
               "\t6. View Account\n"
               "\nEnter your choice: ");
        scanf("%d",&choice);
        printf("\n");

        /* SWITCH START */
        switch(choice)
        {

        /* CASE 0: EXIT PROGRAM */
        case 0:
            exit(1);
            break;

        /* CASE 1: DEPOSIT */
        case 1:
            fopen("accounts.dat", "r+");
            fseek(fp,0,SEEK_SET);
            printf("please enter account number: ");
            scanf("%ld",&aNumber);
            printf("Amount to be deposited: $");
            scanf("%f", &amount);
            for(i=0; i<count; i++)
            {
                if(account[i].aNumber == aNumber)
                {
                    account[i].aBalance = account[i].aBalance + amount;
                    fseek(fp,i * sizeof(struct bank),SEEK_SET);
                    fwrite(account+i,sizeof(struct bank),1, fp);
                    break;
                }
            }
            if(i==count)
                printf("Account number does not exits\n");
            break;

        /* CASE:2 WITHDRAWAL */
        case 2:
            fopen("accounts.dat", "r+");
            fseek(fp,0,SEEK_SET);
            printf("Please enter account number: ");
            scanf("%ld",&aNumber);
            printf("Enter the amount to be withdrawn: $");
            scanf("%f",&amount);
            for(i=0; i<count; i++)
            {
                if(account[i].aNumber==aNumber)
                {
                    if(account[i].aBalance<amount)
                    {
                        printf("Insufficient balance\n");
                        break;
                    }
                    account[i].aBalance=account[i].aBalance-amount;
                    fseek(fp,i*sizeof(struct bank),SEEK_SET);
                    fwrite(account+i,sizeof(struct bank),1, fp);
                    break;
                }
            }
            if(i==count)
                printf("Account number does not exits\n");
            break;

        /* CASE 3: ADD ACCOUNT */
        case 3:
            fp = fopen("accounts.dat","a+");
            printf("Enter a 6-digit account number: ");
            scanf("%ld",&aNumber);
            for(i=0; i<count; i++)
            {
                if(account[i].aNumber==aNumber)
                {
                    printf("Account already exist\n");
                    break;
                }
            }
            if(i==count)
            {
                account[i].aNumber=aNumber;

                printf("Enter the First Name: ");
                scanf("%s",account[i].first);

                printf("Enter the Middle Initial: ");
                scanf("%s",account[i].middle);

                printf("Enter the Last Name: ");
                scanf("%s",account[i].last);

                printf("Enter the Amount: $");
                scanf("%f",&account[i].aBalance);
                fseek(fp,0,SEEK_END);
                fwrite(account+i,sizeof(struct bank),1, fp);
                count++;
            }
            break;

        /* CASE 4: DELETE ACCOUNT */
        case 4:
            fopen("accounts.dat", "a+");
            fseek(fp,0,SEEK_SET);
            printf("Please enter account number: ");
            scanf("%ld",&aNumber);
            for(i=0; i<count; i++)
            {
                if(account[i].aNumber==aNumber)
                    break;
            }
            if(i==count)
                printf("Account number does not exits\n");
            else
            {
                while(i<count)
                {
                    strcpy(account[i].first,account[i+1].first);
                    strcpy(account[i].middle,account[i+1].middle);
                    strcpy(account[i].last,account[i+1].last);
                    account[i].aNumber=account[i+1].aNumber;
                    account[i].aBalance=account[i+1].aBalance;
                }
                count--;
                fp=fopen("accounts.dat", "wb");
                for(i=0; i<count; i++)
                    fwrite(account+i,sizeof(struct bank),1, fp);
                fclose(fp);
                /* REOPEN FILE */
                fopen("accounts.dat", "r+");
            }
            break;

        /* CASE 5: BALANCE INQUIRY*/
        case 5:
            fopen("accounts.dat", "r");
            printf("Please enter account number: ");
            scanf("%ld",&aNumber);
            for(i=0; i<count; i++)
            {
                if(account[i].aNumber==aNumber)
                {
                    printf("First Name: %s\n",account[i].first);
                    printf("Middle Initial: %s\n",account[i].middle);
                    printf("Last Name: %s\n",account[i].last);
                    printf("Account Number: %ld\n",account[i].aNumber);
                    printf("Balance Amount: $%.2f\n",account[i].aBalance);
                    break;
                }
            }
            if(i==count)
                printf("Account number does not exits\n");
            break;

        /* CASE 6: VIEW ACCOUNTS*/
        case 6:
            fopen("accounts.dat", "r");
            for(i=0; i<count; i++)
            {
                fread(&account[i], sizeof(account), 1, fp);
                printf("Entry %1d\n", i+1);
                printf("First Name: %s\n",account[i].first);
                printf("Middle Initial: %s\n",account[i].middle);
                printf("Last Name: %s\n",account[i].last);
                printf("Account Number: %ld\n",account[i].aNumber);
                printf("Account Balance: $%.2f\n",account[i].aBalance);
            }
        } /* END SWITCH-STATEMENT */
    } /* END 'DO' */
    while(choice!=0);
    return 0;
}
/*头文件*/
#包括
#包括
#包括
/*用于保存帐户信息的结构*/
结构银行
{
字符优先[25];
半焦中[25];
最后一个字符[25];
一个月内长;
浮动平衡;
};
/*主要起点*/
int main()
{
int-choice;/*开关语句大小写*/
int i;/*用于循环计数器*/
int count;/*跟踪账户总数*/
long int aNumber;/*暂时保留帐户#用于在文件中搜索*/
浮动金额;/*由于各种原因暂时持有美元金额*/
文件*fp;/*我行的具体文件声明*/
结构银行帐户[50];/*结构的数组声明*/
/*开始时做某事*/
做
{
/*选项菜单*/
printf(“\n\n请访问您的银行。\n”
“请按照下面的提示操作。\n\n”
“\t0.退出\n”
“\t1.存款\n”
“\t2.撤回\n”
“\t3.添加帐户\n”
“\t4.删除帐户\n”
“\t5.余额查询\n”
“\t6.查看帐户\n”
“\n输入您的选择:”;
scanf(“%d”,选择(&C);
printf(“\n”);
/*开关启动*/
开关(选择)
{
/*案例0:退出程序*/
案例0:
出口(1);
打破
/*案例1:押金*/
案例1:
fopen(“accounts.dat”、“r+”);
fseek(fp,0,SEEK_集);
printf(“请输入账号:”);
scanf(“%ld”、&aNumber);
printf(“存款金额:$”;
scanf(“%f”和金额);

对于(i=0;i当您使用案例6启动程序时,
计数未初始化,导致
for
循环出现意外行为。

如前所述,计数尚未初始化,但通常在退出程序并稍后重新启动时以零值开始。虽然这不能保证,如果你会发现循环会提前终止


因此,在重新启动时,您需要做的是读回dat文件中的记录数,并将计数初始化为该数字,然后从该数字开始一切工作。您可能需要创建一个新选项“读入现有文件”或者类似于扫描。

不仅仅是案例6。
i
测试在6个不同的位置使用,但是在整个代码中,
count
从未在单个位置初始化。您必须验证每个
scanf
的返回值,例如
scanf(“%s”,account[i]);
(或该点的任何输入函数)为了确保您实际处理的是有效数据,请检查返回。这远远不是一个.Read。