c - 使用 C 中的结构从文件读取数据时出现错误

标签 c structure file-handling fwrite fread

我正在做一个名为 ATM 的小型项目,作为冬季作业。我已经完成了 ATM 的编码,如下面的代码所示。但我遇到了一个问题,假设我已经为用户创建了一个数据库并将其存储到文件中,但对于第一个用户,即第一个条目,我可以轻松读取数据,但对于文件中的第二个条目,我无法读取文件中的数据第一次尝试。第一次尝试时,它显示错误的 IdPIN,但第二次输入时一切正常。列表中的第三个条目(即第三个用户)也发生了同样的情况。前两次尝试显示错误的 ID 和 PIN 码,但第三次尝试一切正常。对于所有用户都是如此,即第四、第五等。

这是我的代码:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct user
{  
    int id;
    char fname[20],lname[20];
    long unsigned int acct_num, mobile;
    int pin;
    float balance;
};

void balance_enquiry();
void balance_withdraw();
void balance_deposit();
void account_creation();
void account_modify();
void account_auth_id();
void account_auth_pin();

char data[] = {"ATM.dat"};
int main()
{
    int option;
    G:printf("\nWelcome To The ATM Services:\n");
    printf("\n****************************\n");
    printf("\nPlease Choose Your Options:\n");
    printf("\n");
    printf("\n1. Banking Services:\n");
    printf("\n2. Account Creation:\n");
    printf("\n3. Account Modification:\n");
    scanf("%d", &option);
    switch (option)
    {
        case 1:
        {
            system("clear");
            int choice;
            account_auth_id();
            account_auth_pin();
            printf("\n********Welcome to ATM Service**************\n");
            printf("\n1. Check Balance\n");
            printf("\n2. Withdraw Cash\n");
            printf("\n3. Deposit Cash\n");
            printf("\n4. Quit\n");
            printf("\n******************?**************************?*\n\n");
            printf("\nEnter Your Choice: \n");
            scanf("%d", &choice);
            switch (choice)
            {
                case 1:
                    balance_enquiry();
                    break;
                case 2:
                    balance_withdraw();
                    break;
                case 3:
                    balance_deposit();
                case 4:
                    printf("\nTHANK U FOR USING ATM \n");
                    break;
                default:
                    printf("\nINVALID CHOICE:\n");
                    break;
            }
            break;
        }
        case 2:
            account_creation();
            break;
        case 3:
            account_modify();
            break;
        case 4:
            printf("\nTHANK U FOR USING ATM \n");
            break;
        default:
            printf("\nEnter Valid Choice:\n");
            break;
    }
    char transaction;
    printf("\nDO U WANT TO PERFORM ANOTHER TRANSCATION (y or n): \n");
    scanf(" %c", &transaction);
    if (transaction == 'y')
        goto G;
    printf("\nTHANKS FOR USING OUT ATM SERVICE:\n\n");
    getch();
    return 0;
}

void balance_enquiry()
{
    FILE *fp;
    struct user t;
    int id, f = 0;
    fp = fopen(data, "rb");
    printf("\nEnter Your ID Number:\n");
    D:scanf("%d", &id);
    while (1)
    {
        fread(&t, sizeof(t), 1, fp);
        if (feof(fp))
            break;
        if (t.id == id)
        {
            f = 1;
            printf("Your Current Balance Is: %f\n", t.balance);
        }
        if (f == 0)
        {
            printf("Please Enter valid ID:\n");
            goto D;
        }
    }
    fclose(fp);
}

void balance_withdraw()
{
    FILE *fp,*fp1;
    struct user t1;
    int withdraw,id, f = 0;
    fp = fopen("ATM.dat", "rb");
    fp1 = fopen("temp.dat", "wb");
    printf("\nEnter Your ID Number:\n");
    scanf("%d", &id);
    while (1)
    {
        fread(&t1, sizeof(struct user), 1, fp);
        if (feof(fp))
            break;
        if (t1.id == id)
        {
            f = 1;
            printf("\nENTER THE AMOUNT TO WITHDRAW: \n");
            A:scanf("%d", &withdraw);
            if (withdraw % 100 != 0)
            {
                printf("\nPLEASE ENTER THE AMOUNT IN MULTIPLES OF 100:\n");
                    goto A;
            }
            if (withdraw > t1.balance)
            {
                printf("\nINSUFFICENT BALANCE:\n");
                printf("\nTRY WITH LESSER AMOUNT\n");
                goto A;
                if (withdraw <= t1.balance)
                {
                    t1.balance = t1.balance - withdraw;
                    printf("\nPLEASE COLLECT CASH:\n\n");
                    printf("\nYOUR CURRENT BALANCE IS: %d", t1.balance);
                    fwrite(&t1, sizeof(struct user), 1, fp1);
                }
                else
                    fwrite(&t1, sizeof(struct user), 1, fp1);

            }
        }
        fclose(fp);
        fclose(fp1);
        if (f == 0)
        {
            printf("Sorry, There Is Some Technical Problem:\n\n");
        }
        else
        {
            fp = fopen("ATM.dat", "wb");
            fp1 = fopen("temp.dat", "rb");
            while (1)
            {
                fread(&t1, sizeof(struct user), 1, fp1);
                if (feof(fp1))
                    break;
                fwrite(&t1, sizeof(struct user), 1, fp);
            }
        }
        fclose(fp);
        fclose(fp1);
    }
}

void balance_deposit()
{

}

void account_auth_id()
{
    FILE *fp;
    struct user t;
    int id, f = 0,k =0;
    fp = fopen(data, "rb");
    fflush(stdin);
    A:printf("\nEnter Your ID Number:\n");
    scanf("%d", &id);
    while (1)
    {
        fread(&t, sizeof(t), 1, fp);
        if (feof(fp))
            break;
        if (t.id == id) 
        {
            f = 1;
            printf("Welcome, Mr %s\n", t.fname);
        }
        if (f == 0) 
        {
            k++;
            if(k == 3)
            {
                printf("You Have Reached Max. Attempts:\n");
                getch();
                return 0;
            }
            printf("You Have Entered Wrong ID:\n");
            printf("Please Enter Valid ID:\n");
            goto A;
        }
    }

}

void account_auth_pin()
{
    FILE *fp;
    struct user t;
    int tmpPin,k = 0, f = 0;
    fp = fopen(data, "rb");
    fflush(stdin);
    B:printf("Enter Your Secret Pin:\n");
    scanf("%d", &tmpPin);
    while (1)
    {
        fread(&t, sizeof(t), 1, fp);
        if (feof(fp))
            break;
        if (t.pin == tmpPin)
        {
            f = 1;
            printf("Super!,Now You Can Enjoy Banking Services:\n");
        }
        if (f == 0)
        {
            k++;
            if(k == 3)
            {
                printf("You Have Entered Three Times Wrong PIN:\n");
                printf("Please,Try Again:\n");
                getch();
                return 0;
            }
            printf("You Have Entered Wrong PIN:\n");
            printf("Please Enter Correct PIN:\n");
            goto B;
        }
    }

}

void account_creation()
{
    FILE *fp;
    struct user t;
    fp = fopen(data, "ab");
    printf("Enter Your ID Number:\n");
    scanf("%d", &t.id);
    printf("\nEnter Your First Name:\n");
    scanf("%s", &t.fname);
    printf("\nEnter Your Last Name:\n");
    scanf("%s", &t.lname);
    printf("Enter Your New Account number:\n");
    scanf("%lu", &t.acct_num);
    printf("Enter Your Mobile Number:\n");
    scanf("%lu", &t.mobile);
    printf("Enter Your Secret PIN:\n");
    scanf("%d", &t.pin);
    printf("Enter The initial Amount To Deposit:\n");
    scanf("%f", &t.balance);
    fwrite(&t, sizeof(t), 1, fp);
    fclose(fp);
}

void account_modify()
{

}

最佳答案

最后,我的代码可以正常工作了..现在一切都很好。 这是我的代码,供那些对此项目或类似项目感兴趣的人使用。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct user
{
    int id;
    char name[20];
    char lname[20];
    int acct_num;
    int pin;
    int balance;
};
void balance_enquiry();
void balance_withdraw();
void balance_deposit();
void account_creation();
void account_modify();
void account_del();
void displayAll();

char fname[] = { "atm.dat" };

int main()
{
    while (1)
    {
        int ch;
        printf("\n\t\tWelcome To The ATM Services:\n");
        printf("\n\t\t****************************\n");
        printf("\n\t\tPlease Choose Your Options:\n");
        printf("\n");
        printf("1. Balance Enquiry:\t\t4. Account creation:\n");
        printf("2. Withdraw Money:\t\t5. Account Modification:\n");
        printf("3. Deposit Money:\t\t6. Account Delete:\n\n");
        printf("\t\tPress 0 For Exit:\n");
        scanf("%d", &ch);
        switch (ch)
        {
        case 1:
            balance_enquiry();
            break;
        case 2:
            balance_withdraw();
            break;
        case 3:
            balance_deposit();
            break;
        case 4:
            account_creation();
            break;
        case 5:
            account_modify();
            break;
        case 6:
            account_del();
            break;
        case 0:
            return 0;
        case 11://to see all details of customer in database.
            displayAll();
            break;
        default:
            printf("Enter Valid Options:\n");
        }
    }
    return 0;
}
void balance_enquiry()
{
    FILE *fp;
    struct user t;
    int id, tmpPin, found = 0;
    fp = fopen(fname, "rb");
    printf("\nEnter Your Customer ID:");
    scanf("%d", &id);
    printf("\nEnter Your Secret PIN:\n");
    scanf("%d", &tmpPin);
    while (1)
    {
        fread(&t, sizeof(t), 1, fp);

        if (feof(fp))
        {
            break;
        }
        if (t.id == id && t.pin == tmpPin)
        {
            found = 1;
            printf("\n============================================\n\n");
            printf("Welcome, Mr %s %s\n\n", t.name, t.lname);
            printf("Account Number\t\t Balance\n\n");
            printf("%d\t\t %d\n\n", t.acct_num, t.balance);
            printf("==============================================\n\n");
        }
    }
    if (found == 0)
    {
        printf("\nSorry You Have Entered Wrong Details:\n");
    }
    fclose(fp);
}
void balance_withdraw()
{
    FILE *fp, *fp1;
    struct user t;
    int id, found = 0;
    int withdraw, tmpPin;
    fp = fopen(fname, "rb");
    fp1 = fopen("temp.dat", "wb");

    printf("\nEnter the Customer ID:\n");
    scanf("%d", &id);
    printf("Enter Your Secret PIN:\n");
    scanf("%d", &tmpPin);
    while (1)
    {
        fread(&t, sizeof(t), 1, fp);
        if (feof(fp))
        {
            break;
        }
        if (t.id == id && t.pin == tmpPin)
        {
            found = 1;
            do
            {
                do
                {
                    printf("\nEnter Amount To Withdraw:\n");
                    scanf("%d", &withdraw);
                    if (withdraw % 100 != 0)
                    {
                        printf("\nAmount Should Be Multiple Of 100:\n");
                    }
                } while (withdraw % 100 != 0);
                if (withdraw > t.balance)
                {
                    printf("\n Insufficient Balance:\n");
                    printf("\nTry With Lesser Amount\n");
                }
            } while (withdraw > t.balance);
            if (withdraw <= t.balance)
            {
                t.balance = t.balance - withdraw;
                printf("\nPlease Collect Your Cash:\n\n");
                printf("\nYour Current Balance Is: %d", t.balance);
                fflush(stdin);
                fwrite(&t, sizeof(t), 1, fp1);
            }
        }
        else
        {
            fwrite(&t, sizeof(t), 1, fp1);
        }
    }
    fclose(fp);
    fclose(fp1);
    if (found == 0)
    {
        printf("Sorry Try Again! Technical Problem:\n\n");
    }
    else
    {
        fp = fopen(fname, "wb");
        fp1 = fopen("temp.dat", "rb");
        while (1)
         {
            fread(&t, sizeof(t), 1, fp1);
            if (feof(fp1))
            {
                break;
            }
            fwrite(&t, sizeof(t), 1, fp);
        }

    }
    fclose(fp);
    fclose(fp1);
}
void balance_deposit()
{
    FILE *fp, *fp1;
    struct user t;
    int id, found = 0;
    int deposit, tmpPin;
    fp = fopen(fname, "rb");
    fp1 = fopen("temp.dat", "wb");

    printf("\nEnter the Customer ID:\n");
    scanf("%d", &id);
    printf("Enter Your Secret PIN:\n");
    scanf("%d", &tmpPin);
    while (1)
    {
        fread(&t, sizeof(t), 1, fp);
        if (feof(fp))
        {
            break;
        }
        if (t.id == id && t.pin == tmpPin)
        {
            found = 1;
            do
            {
                printf("\nEnter Amount To Deposit:\n");
                scanf("%d", &deposit);
                if (deposit % 100 != 0)
                {
                    printf("\nPLEASE Enter The Amount In Multiples Of 100:\n");
                }
            } while (deposit % 100 != 0);
            t.balance = t.balance + deposit;
            printf("\nYOUR CURRENT BALANCE IS: %d", t.balance);
            fflush(stdin);
            fwrite(&t, sizeof(t), 1, fp1);
        }
        else
        {
            fwrite(&t, sizeof(t), 1, fp1);
        }
    }
    fclose(fp);
    fclose(fp1);
    if (found == 0)
    {
        printf("Sorry Try Again! Technical Problem:\n\n");
    }
    else
    {
        fp = fopen(fname, "wb");
        fp1 = fopen("temp.dat", "rb");

        while (1)
        {
            fread(&t, sizeof(t), 1, fp1);
            if (feof(fp1))
            {
                break;
            }
            fwrite(&t, sizeof(t), 1, fp);
        }

    }
    fclose(fp);
    fclose(fp1);
 }
void account_creation()
{
    FILE *fp;
    struct user t;
    fp = fopen(fname, "ab");
    printf("\t\tWelcome,First Time User\n\nPlease Enter Your Personal Details:\n\n");
    printf("Enter Customer ID Number:\n");
    scanf("%d", &t.id);
    printf("Enter You First Name:\n");
    scanf("%s", t.name);
    printf("Enter Your Last Name:\n");
    scanf("%s", t.lname);
    printf("Enter Your New Account Number:\n");
    scanf("%d", &t.acct_num);
    printf("Enter Your Secret PIN:\n");
    scanf("%d", &t.pin);
    printf("Enter The Initial Amount:\n");
    scanf("%d", &t.balance);
    fwrite(&t, sizeof(struct user), 1, fp);
    fclose(fp);
}
void account_modify()
{
    FILE *fp, *fp1;
    struct user t;
    int id, found = 0;
    int tmpPin, ch;
    fp = fopen(fname, "rb");
    fp1 = fopen("temp.dat", "wb");
    printf("\nEnter the Customer ID you want to Modify:");
    scanf("%d", &id);
    printf("\nEnter the Secret PIN of Custmer ID = %d:\n", id);
    scanf("%d", &tmpPin);
    while (1)
    {
        fread(&t, sizeof(t), 1, fp);

        if (feof(fp))
        {
            break;
        }
        if (t.id == id && t.pin == tmpPin)
        {
            found = 1;
            printf("Which Data You Want To Modify:\n");
            printf("\t1. Custormer ID:\n");
            printf("\t2. First Name:\n");
            printf("\t3. Last Name:\n");
            printf("\t4. Secret PIN:\n");
            printf("Others are Not Allowed:\n");
            scanf("%d", &ch);
            switch (ch)
            {
             case 1:
                printf("Enter New customer ID:");
                scanf("%d", &t.id);
                fflush(stdin);
                break;
            case 2:
                printf("Enter New Customer First Name:\n");
                scanf("%s", t.name);
                break;
            case 3:
                printf("Enter New Customer Last Name:\n");
                scanf("%s", t.lname);
                break;
            case 4:
                printf("Enter Your New Secret PIN:\n");
                scanf("%d", &t.pin);
                break;
            default:
                printf("tEnter Valid Choice:\n");
                break;
            }
            fwrite(&t, sizeof(t), 1, fp1);
        }
        else
        {
            fwrite(&t, sizeof(t), 1, fp1);
        }
    }
    fclose(fp);
    fclose(fp1);
    if (found == 0)
    {
        printf("Sorry No Record Found\n\n");
    }
    else
    {
        fp = fopen(fname, "wb");
        fp1 = fopen("temp.dat", "rb");
        while (1)
        {
            fread(&t, sizeof(t), 1, fp1);
            if (feof(fp1))
            {
                break;
            }
            fwrite(&t, sizeof(t), 1, fp);
        }
    }
    fclose(fp);
    fclose(fp1);
}

void account_del()
{
    FILE *fp, *fp1;
    struct user t;
    int id, found = 0;

    fp = fopen(fname, "rb");
    fp1 = fopen("temp.dat", "wb");

    printf("\nEnter the Customer ID you want to Delete:");
    scanf("%d", &id);
    while (1)
    {
        fread(&t, sizeof(t), 1, fp);
        if (feof(fp))
        {
             break;
        }
        if (t.id == id)
        {
            found = 1;
        }
        else
        {
            fwrite(&t, sizeof(t), 1, fp1);
        }
    }
    fclose(fp);
    fclose(fp1);
    if (found == 0)
    {
        printf("Sorry No Record Found\n\n");
    }
    else
    {
        fp = fopen(fname, "wb");
        fp1 = fopen("temp.dat", "rb");
        while (1)
        {
            fread(&t, sizeof(t), 1, fp1);
            if (feof(fp1))
            {
                break;
            }
            fwrite(&t, sizeof(t), 1, fp);
        }
    }
    fclose(fp);
    fclose(fp1);
}
void displayAll()
{
    FILE *fp;
    struct user t;
    fp = fopen(fname, "rb");
    printf("\n========================================================\n\n");
    printf("\t\t All Customer Details\n\n");
    printf("=====================================================\n\n");
    printf("ID\tFirst\tLast\tAccount Number\tPIN\tBalance\n\n");
    while (1)
    {
        fread(&t, sizeof(struct user), 1, fp);
        if (feof(fp))
        {
            break;
        }
        printf("%d\t", t.id);
        printf("%s\t", t.name);
        printf("%s\t", t.lname);
        printf("%d\t", t.acct_num);
        printf("%d\t", t.pin);
        printf("%d\t\n\n", t.balance);
    }
    printf("=====================================================\n\n");
    fclose(fp);
}

关于c - 使用 C 中的结构从文件读取数据时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48431667/

相关文章:

c - 循环中守卫的数组索引?它实际上检查什么?

c - 骰子滚动模拟器,2 个骰子

c - 如何在c中对常见数据成员/变量进行分组?

c++ - 我应该使用 C 类型 (uint8_t/.../uint64_t) 还是 (u_int8_t/.../u_int64_t)?

共享内存中的c结构

java - 如何根据限制 : java 列出文件系统中的文件

我们可以在 fread 函数中使用 0 地址吗

c - 函数中结构数组的处理成员

php - 为什么 PHP 认为它正在写入文件,但事实并非如此?

c - 如何在 YACC 语法中回显输入文本?