我正在尝试执行一种搜索方法,其中检查 2 个仅存储整数的 ID。
首先,我有一个客户数据库。我按此顺序依次输入姓名、姓氏、身份证和地址,然后将这些立即保存到文件中
当用户输入身份证时,这会调用此搜索方法并检查所有文件以查看 ID 是否唯一。如果不是,则返回 0,否则返回 1
现在的问题是这样的。当我输入 ID 时,无论它是否唯一,它都会继续运行,但是当它输出我写的内容时,对于 NAME 和 SURNAME,它只显示我存储在那里的第一条记录(就像卡在某种缓冲区),ID和地址正常输出。
该文件也未更新的意思是,保存文件没有发生。现在,当我删除此方法时,附加工作正常,但我无法访问 ID 的比较。
有什么建议为什么会发生这种情况?如果可能的话,我知道如何解决它?就像每当我使用这种搜索方法时,整个文件都会从头开始并卡在那里。我尝试使用带有布尔值的方法,但仍然无济于事。当我尝试将它与布尔值而不是“if (customerID(scanf ("%d",&cCheck)) == 1)”行一起使用时,我将其设置为 == TRUE,它给了我一个错误,即输出将始终为== FALSE,因为数据不为 NULL。
哦 TRUE 和 FALSE 在我的情况下是有效的,因为我在 common.h 中有一个 typedef enum boolean
代码如下[发布整个文件]: 相关方法是[ void addCustomer()] 和[int customerID (int cCheck) 但我发布了所有这些方法,因为它们中的一些是互连的。
编辑!!!- 即使 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;
}
编辑!!
如果我不清楚,上传图片以显示我的意思
(注意名字和姓氏与那些输入的不同)!http://s017.radikal.ru/i443/1212/c8/1ea9bc56d980.jpg
以下显示了我在文件中的内容(只有一个文件)!http://s017.radikal.ru/i431/1212/49/2a0df6acf9ec.jpg