简体   繁体   English

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

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

It's my first time asking here, so I'm sorry in advance if my post is a bit messy. 这是我第一次在这里问问题,所以对不起,如果我的帖子有点乱,我要先抱歉。 I'm a freshman and my finals is to make an atm program. 我是一名新生,我的决赛是进行atm程序。

My program uses switch statements for the options: the first one asks the user for their info, (account number, account name, PIN, initial deposit), while the second one is for the actual transactions: Balance check, Deposit and Withdrawal. 我的程序使用switch语句作为选项:第一个询问用户的信息(帐号,帐户名,PIN,初始存款),第二个询问实际交易:余额检查,存款和取款。 Before you could do any one of those options, of course I'll have to check if the info (in this case the account number and PIN) matches the ones in my file. 当然,在您可以执行这些选项中的任何一个之前,我必须检查信息(在这种情况下,帐号和PIN)是否与我的文件中的信息匹配。

The file is in binary. 该文件为二进制文件。 My problem is that my code for reading the file works fine if i want to display the contents of my file (all the data entries that I populated the file with are shown), but when i want to use the contents of the file (searching inside the file for a match of the user inputted account number) it only reads the first line of the file, thus only the first data entry works, while the one's after that are not read. 我的问题是,如果我想显示文件的内容(显示了填充文件的所有数据条目),但是我想使用文件的内容(搜索时),则用于读取文件的代码可以正常工作在文件中查找与用户输入的帐号匹配的文件),它仅读取文件的第一行,因此仅第一个数据条目有效,而后面的数据则不被读取。

My question is, what am i doing wrong in my code? 我的问题是,我的代码在做什么错? its confusing because if I change my code to display the contents it shows everything, meaning it reads the whole file. 它令人困惑,因为如果我更改代码以显示内容,它将显示所有内容,这意味着它将读取整个文件。 but when i want to search the file using a conditional statement, only the first data entry gets read. 但是当我想使用条件语句搜索文件时,仅读取第一个数据条目。 Thank you very much for your time 非常感谢您的宝贵时间

tldr: can't use data in file for conditionals (verify the data inside the file, cuz my code doesn't read it whole apparently, except for the first entry) but if i print the contents it reads it fully tldr:不能使用文件中的数据作为条件文件(验证文件中的数据,因为我的代码除了第一个条目以外,显然没有完整地读取它),但是如果我打印内容,它将完全读取它

my outputs 1.My data entries data entries 2.Entering the Account no. 我的输出1.我的数据条目数据条目 2.输入帐号。 of the 1st one (desired output) desired output 3.Entering The account no. 输入第一个(所需的输出) 所需输出 3。 of the 2nd one (Problem part) problem 第二个(问题部分) 问题

my main code 我的主要代码

#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");

    }

} 

my structure 我的结构

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

code for finding a match of account number in my file (the part i'm having trouble with) 在我的文件中查找帐号匹配的代码(我遇到问题的部分)

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

The expected output is that my conditional statement works properly and reads my whole file for matches. 预期的输出是我的条件语句正常工作,并读取我的整个文件以进行匹配。

try to change the exit(1) to continue; 尝试将exit(1)更改为continue; , the exit(1) will make your program quit the while loop and exit the program immediately whenever accno != accrec.no . exit(1)将使您的程序退出while循环,并在accno != accrec.no时立即退出程序。

in fact, you don't need the else statement inside the while loop, you should put it outside the while loop. 实际上,您不需要在while循环内使用else语句, while应将其放在while循环外。 for example: 例如:

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