繁体   English   中英

在条件语句中使用二进制文件中的数据

[英]Using data inside a binary file in a conditional statement

这是我第一次在这里问问题,所以对不起,如果我的帖子有点乱,我要先抱歉。 我是一名新生,我的决赛是进行atm程序。

我的程序使用switch语句作为选项:第一个询问用户的信息(帐号,帐户名,PIN,初始存款),第二个询问实际交易:余额检查,存款和取款。 当然,在您可以执行这些选项中的任何一个之前,我必须检查信息(在这种情况下,帐号和PIN)是否与我的文件中的信息匹配。

该文件为二进制文件。 我的问题是,如果我想显示文件的内容(显示了填充文件的所有数据条目),但是我想使用文件的内容(搜索时),则用于读取文件的代码可以正常工作在文件中查找与用户输入的帐号匹配的文件),它仅读取文件的第一行,因此仅第一个数据条目有效,而后面的数据则不被读取。

我的问题是,我的代码在做什么错? 它令人困惑,因为如果我更改代码以显示内容,它将显示所有内容,这意味着它将读取整个文件。 但是当我想使用条件语句搜索文件时,仅读取第一个数据条目。 非常感谢您的宝贵时间

tldr:不能使用文件中的数据作为条件文件(验证文件中的数据,因为我的代码除了第一个条目以外,显然没有完整地读取它),但是如果我打印内容,它将完全读取它

我的输出1.我的数据条目数据条目 2.输入帐号。 输入第一个(所需的输出) 所需输出 3。 第二个(问题部分) 问题

我的主要代码

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<stdlib.h>
#include<string.h>
#include<process.h>

struct account
{
    int no;
    char name[100];
    int pin;
    float id;
}; 

main()
{    
    FILE *fptr;
    fptr = fopen("accrec.dat","ab");
    if (fptr == NULL)
    {
        printf("File does not exists \n");
        return 0;
    }
    system("cls");
    struct account accrec;
    int i,tpin[4];
    char step1,ch;

    printf("\t\tWelcome to Banking System\n\t");
    printf("\nA.Open an Account\n");
    printf("\nB.Bank Transaction\n");
    printf("\nC.exit\n");

    scanf("%c",&step1);
    switch (step1)
    {

        case 'A':
            printf("Open a New Account\n");
            printf("\nEnter the following information\n");

            printf(" \n5-digit  Account number:");
            scanf("%d",&accrec.no);

            getchar();
            printf("\nAccount Name:");
            scanf("%[^\n]s",&accrec.name);

            printf("\n4-digit Account PIN:");
                /*for(i=0;i<4;i++)
                {
                ch = getch();
                tpin[4] = ch;
                ch = '*' ;
                printf("%c",ch);
                }mask works but does not allow PIN saving     */
            scanf("%d",&accrec.pin);


            printf("\nInitial deposit:");
            scanf("%f",&accrec.id);

            fwrite(&accrec,sizeof(struct account),1,fptr);

            fclose(fptr);

            break;

        case 'B':
            {
            fptr = fopen("accrec.dat","rb");
            int choice;
            int accno = 0;
            printf("Enter Your Account no.");
            scanf("%d",&accno);
            while (fread(&accrec,sizeof(account),1,fptr)!=NULL)
            {
            if(accno == accrec.no)
            {
            printf("\tWelcome to PUPQC Banking System\n");
            printf("1.Balance Inquiry\n");
            printf("2.Deposit\n");
            printf("3.Withdrawal\n");
            printf("4.quit\n");
            scanf("%d",&choice);
            }
            else 
            {
                printf("account doesn't exist\n");
                exit(1);
            }   


            fclose(fptr);

            switch (choice)
            {
                case 1:

                    printf("BALANCE INQUIRY\n");
                    printf("Current Balance:");
                    fptr = fopen("accrec.dat","rb");
                    if (fptr == NULL)
                    {
                    printf("File Cant be read");
                    exit(1);
                    }

                    printf("Account No: %d\n",accrec.no);

                    while(fread(&accrec,sizeof(struct account),1,fptr)!=NULL);
                    {
                    printf("Initial Deposit is %0.2f\n",accrec.id);
                    }       
                    printf("%d\n",&accrec.id);

                    break;

                case 2:
                    float dv;
                    printf("DEPOSIT\n");
                    printf("Current Balance:");
                    fptr = fopen("accrec.dat","rb");
                    if (fptr == NULL)
                    {
                    printf("File Cant be read");
                    exit(1);
                    }
                    while(fread(&accrec,sizeof(struct account),1,fptr)!=NULL);
                    {
                    printf("%0.2f\n",accrec.id);
                    }
                    printf("Enter amount to deposit:\n");
                    printf("Deposit Value:");
                    scanf("%0.2f",&dv);
                    accrec.id = accrec.id + dv; 
                    fwrite(&accrec,sizeof(struct account),1,fptr);  
                    fclose(fptr);
                    break;

                case 3:
                    float wv;
                    printf("WITHDRAWAL\n");
                    printf("Current Balance:");
                    fptr = fopen("accrec.dat","rb+");
                    if (fptr == NULL)
                    {
                    printf("File Cant be read");
                    exit(1);
                    }
                    while(fread(&accrec,sizeof(struct account),1,fptr)!=NULL);
                    {
                    printf("%0.2f\n",accrec.id);
                    }
                    printf("Enter amount to withdraw:\n");
                    printf("Withdrawal Value:");
                    scanf("%0.2f",wv);
                    accrec.id = accrec.id - wv; 
                    fwrite(&accrec,sizeof(struct account),1,fptr);  
                    fclose(fptr);

                    break;

                case 4:
                    printf("thank you for your patronage \n");
                    return 0;
                    break;

                default:
                    printf("error 404");
                    break;
                }
            }
        }
        break;

        case 'C':
            printf("Thank you! Have a good day");
            return 0;
            break;

        case 'D':
            fptr = fopen("accrec.dat","rb");
            if (fptr == NULL)
            {
                printf("File Cant be read");
                exit(1);
            }

            printf("Data From file\n");

            while(fread(&accrec,sizeof(struct account),1,fptr)!=NULL)
            printf("\n acc.no is %d\n acc.name is %s\n PIN %d\n Initial Deposit is %0.2f\n ",accrec.no,accrec.name,accrec.pin,accrec.id);
            fclose(fptr);
            getch();

            break;

        default:
            printf("invalid input! please select form the options given\n");

    }

} 

我的结构

    struct account
    {
        int no;
        char name[100];
        int pin;
        float id;
    }; 

在我的文件中查找帐号匹配的代码(我遇到问题的部分)

    fptr = fopen("accrec.dat","rb");
    int choice;
    int accno = 0;
    printf("Enter Your Account no.");
    scanf("%d",&accno);
    while (fread(&accrec,sizeof(account),1,fptr)!=NULL)
    {
        if (accno == accrec.no)
        {
            printf("\tWelcome to Banking System\n");
            printf("1.Balance Inquiry\n");
            printf("2.Deposit\n");
            printf("3.Withdrawal\n");
            printf("4.quit\n");

            scanf("%d",&choice);
        }
        else 
        {
            printf("account doesn't exist\n");
            exit(1);
        }   
    }

    fclose(fptr);

预期的输出是我的条件语句正常工作,并读取我的整个文件以进行匹配。

尝试将exit(1)更改为continue; exit(1)将使您的程序退出while循环,并在accno != accrec.no时立即退出程序。

实际上,您不需要在while循环内使用else语句, while应将其放在while循环外。 例如:

scanf("%d",&accno);
boolean found = false;
while (fread(&accrec,sizeof(account),1,fptr)!=NULL)
{
    if (accno == accrec.no)
    {
        found = true;
        scanf("%d",&acpin);*/
        printf("\tWelcome to Banking System\n");
        printf("1.Balance Inquiry\n");
        printf("2.Deposit\n");
        printf("3.Withdrawal\n");
        printf("4.quit\n");

        scanf("%d",&choice);
    }
}
if (!found) {
    printf("account doesn't exist\n");
}

fclose(fptr);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM