搜索方法失败

时间:2012-12-20 22:39:43

标签: c file search identifier

我正在尝试执行搜索方法,其中检查2个仅存储整数的ID。

首先,我有一个客户数据库。我按顺序输入名称,姓氏,ID和地址,然后立即保存到文件中

当用户输入ID卡时,它会调用此搜索方法并检查所有文件以查看该ID是否唯一。如果不是,则返回0,否则返回1

现在的问题是这个。 当我输入ID时,它是否是唯一的,它继续前进,但是当它输出我写的内容时,对于NAME和SURNAME,它只显示我存储在那里的第一条记录(比如卡在某种中)缓冲区),ID和地址输出正常。

文件也未更新意味着,未发生保存文件。 现在,当我删除此方法时,附加功能正常,但我无法访问ID的比较。

为什么会发生这种情况的任何建议?如果可能的话,我知道如何解决它? 它就像我在做这个搜索方法时一样,整个文件从一开始就开始运行并且卡在那里。我尝试使用布尔值的方法,但仍无济于事。当我尝试使用布尔而不是行" if(customerID("%d",& cCheck))== 1)"我做了== TRUE,它给了我一个输出错误 总是== FALSE,因为数据不是NULL。

哦,TRUE和FALSE在我的情况下是有效的,因为我在common.h中有一个typedef枚举布尔值

代码如下[发布整个文件]: 相关方法是[void addCustomer()]和[int customerID(int cCheck) 但是我发布了所有内容,因为其中一些是互连的。

EDIT !!! - ID即使它们不是唯一的,它仍然是接受的......

/*
 * CustomerMainMenu.c
 * Author: DodoSerebro
 *
 * This class will output the Customer's Main Menu and re-directs to the
 * corresponding section
 *
 */
#include<io.h>
#include<fcntl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "..\Headers\common.h"
#include "..\Headers\customerManagement.h"

static FILE *cfp;
static customer c;
#define STRUCTSIZE sizeof (customer)


/** This is the Customers's Main Menu in which the various sections can be
 *  accessed from here
 */
boolean  customerMainMenu()
{



    int optionC;
    clrscr();

    copyright();


    printf ("\n\n\n\n\t\t    ************* Customer's Main Menu *************\n \n \n");

    printf ("Press [1] to add a new Customer\n");
    printf ("Press [2] to edit a Customer\n");
    printf ("Press [3] to list all Customers\n");
    printf ("Press [4] to Show a Customer's last Order\n");
    printf ("Press [5] to go back to Main Menu\n\n\n");


    if (scanf ("%d",&optionC) == 1)
    {
        switch (optionC)
        {

        case 1:
        {
            clrscr();
            getchar();
            addCustomer();
            break;
        }
        case 2:
        {
            printf ("Edit a Customer\n");
            break;
        }

        case 3:
        {
            clrscr();
            listCustomers();
            system ("PAUSE");
            break;
        }
        case 4:
        {
            printf ("Customer's Last Order\n");
            break;
        }
        case 5:
        {
            system ("PAUSE");
            break;
        }
        default:
        {
            if (optionC != 1 || optionC != 2 || optionC != 3 || optionC != 4 || optionC !=5)
            {
                clrscr();
                printf ("Invalid option!\n");
                system ("PAUSE");
                customerMainMenu();
            }
            break;
        }
        }
    }
    return TRUE;

}

/**
 *  This following method will append a customer to the
 *  database at the end of the file
 *
 *  */

void addCustomer ()
{
    char ch;
    copyright();

    printf ("\n\n\n\n\t\t    ************* Add Client **********\n \n \n");

    if ((cfp = fopen ("customers.dat","a+b")) == NULL)
    {
        fputs("Can't open customers.dat file\n",stderr);
    }



    printf ("\tThis will add another customer to the the database\n");
    printf ("\tPress 'Y' to confirm or 'N' to return to the Client Main Menu\n\tWITHOUT adding a customer\n");
    ch = getchar();

    if (ch == 'n' || ch == 'N')
    {
        customerMainMenu();
    }
    else if (ch == 'y' || ch == 'Y')
    {
        fflush(stdin);
        clrscr();
        printf ("\n\n\n\n\t\t    ************* Add Client **********\n \n \n");
        printf ("Please enter Name:\n");
        while (scanf ("%s", c.name) == 0 || cCheck(c.name,100) == FALSE);
        {

        }


        printf ("Please Enter Surname: \n");
        while (scanf ("%s",c.surname) == 0 && cCheck (c.surname,100) == FALSE);
        {

        }
        printf ("Please Enter ID Card, [NOTE! Only numbers are allowed!]\n");
        int cCheck;
        if (customerID(scanf ("%d",&cCheck)) == 1)
        {
            printf ("ID already Taken, Client exists!\n");
            printf ("Do you want to enter another ID? 'Y' for Yes and 'N' to return to Main Menu\n");
            ch = getchar();
            if (ch == 'Y' || ch == 'y')
            {
                scanf ("%d",&cCheck);
                customerID(cCheck);
                c.ID = cCheck;
            }
            else
            {
                customerMainMenu();
            }
        }
        else
        {
            c.ID = cCheck;
        }


        getchar();

        printf ("Please Enter Address:\n");
        gets(c.address);


        fwrite (&c,STRUCTSIZE, 1, cfp);

        printf ("For Testing purposes:\n");
        printf (" %s\n %s\n %s\n %d\n", c.name, c.surname, c.address, c.ID);
        askAnother();


    }
    else
    {
        printf ("\nInvalid choice! Either Y or N is accepted\n");
        system ("PAUSE");
        getchar();
        clrscr();
        addCustomer();
    }
}

void listCustomers()
{

    if ((cfp = fopen ("customers.dat","rb")) == NULL)
    {
        fputs("Can't open customers.dat file\n",stderr);
        printf ("Returning to Customer Main Menu");
        system ("PAUSE");
        customerMainMenu();
    }

    rewind (cfp);
    while (fread (&c,STRUCTSIZE,1,cfp)==1)
    {
        printf ("Customer: %s %s ID: %d\n", c.surname, c.name, c.ID);
    }
    fclose (cfp);


}


void askAnother()
{
    printf ("Do you want to add another Customer?\n");
    printf ("Enter 'Y' for yes and 'N' to return to the Main Menu\n");

    char input;
    input = getchar();

    if (input == 'Y' || input == 'y')
    {
        getchar();
        addCustomer();
    }
    else if (input == 'N'|| input == 'n')
    {

        fclose (cfp);
        customerMainMenu();

    }
    else
    {

        printf ("Invalid Option! Only Y or N are allowed\n");
        system ("PAUSE");
        clrscr();
        askAnother();

    }

}


boolean cCheck(char *test, int max)
{
    int x;
    for (x =0; x<max; x++)
    {
        if (isdigit(test[x]))
        {
            return FALSE;
        }
        if (x==max)
        {
            return TRUE;
        }
        x++;

    }
    return TRUE;
}

/**
 *  This method will compare the ID passed from the ID of the customer to check
 *  whether it is exists or not. If it exists it will output 1 otherwise it
 *  will output -1. This will make sure that the Person's ID is unique
 *
 */


int customerID (int cCheck)
{

    rewind (cfp);
    while (fread (&c,STRUCTSIZE,1,cfp)==1)
    {
        if (c.ID == cCheck)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }

    return 1;
}

EDIT !!

上传图片以显示我的意思,如果我不清楚

(注意名称和姓氏与那些输入的不同之处) !HTTP://s017.radikal.ru/i443/1212/c8/1ea9bc56d980.jpg

以下显示了我在文件中的内容 (只有一个文件) !HTTP://s017.radikal.ru/i431/1212/49/2a0df6acf9ec.jpg

1 个答案:

答案 0 :(得分:0)

if (customerID(scanf ("%d",&cCheck)) == 1)

在这里,你是scanf()返回值customerID(),而你实际上想要传递扫描值cCheck

您可以将调用功能和输入读数分开,如下所示:

   if(scanf ("%d",&cCheck) != 1) {
   // Input error handling
   }
   else if (customerID(cCheck) == 1) {
    ...

另一个问题是,你有一个名为cCheck的函数,你还有一个名为cCheck的局部变量。适当地重命名其中一个。