gpt4 book ai didi

c - 如何用C编程检查文件中的ID是否已存在

转载 作者:行者123 更新时间:2023-11-30 14:22:14 26 4
gpt4 key购买 nike

我在下面有这个代码(仅发布客户管理)。

这是一个数据库,每次通过时我都会在其中添加一个客户。现在我需要检查客户端ID c.ID 是否存在。

我尝试执行一个名为 searchID 的方法,如果找到则返回 1,如果未找到则返回 -1。问题是当我尝试运行该程序时,该程序实际上卡在那里。无论我按 23 还是“ENTER”都不会发生,我需要使用 CTRL + C 退出它;

这就是它的工作原理:

当我添加客户(这是一个结构)时,它会保存到文件中,但我首先需要检查该ID是否存在于数据库中,否则我需要要求用户输入另一个ID或返回主菜单

请问有什么建议吗?谢谢

#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();
getchar();
while (getchar()!='\n')
{

}
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')
{

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 cID;
cID = 0;
while (scanf ("%d",&cID)==0)
{
printf ("Only Numbers are allowed!\n");
while (getchar() != '\n')
{
}
}

if (searchID(cID) == 1)
{
printf ("This ID already exists. Client already exists!\n");
printf ("Do you want to input another ID or return to Main Menu?\n");
printf ("Press 'Y' if you enter another ID, press any other key to return to Main Menu\n:");

ch = getchar();
if (ch == 'y' || ch == 'Y')
{
printf ("Enter another ID:\n");
while (scanf ("%d",&cID)==0)
{
printf ("Only Numbers are allowed!\n");
while (getchar() != '\n')
{
}
}
searchID(cID);
}
else if (searchID(cID) == -1)
{
cID = c.ID;
getchar();
}

}



while (getchar()!='\n')
{

}

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();
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);
// system ("PAUSE");

}


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

int fileSize()
{
int lengthOfFile;
int file;

file = open("Customers.dat",O_RDONLY,0);
lengthOfFile = lseek (file,0, SEEK_END);

return lengthOfFile;
}

int getNoOfRecords()
{
return (fileSize()/(STRUCTSIZE));
}

/**
* 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 searchID (int cID)
{
// for the while loop
int index;
index = 0;
//gets the number of records currently held in the file.
int records;
records = getNoOfRecords();

//User will input the ID into this variable and it will be checked
//whether it exists or not


int IDstatus;
IDstatus = 0;

while (index != records)
{
fread (&c,STRUCTSIZE,1,cfp);
if (c.ID == cID)
{
IDstatus = 1;
}
else
{
IDstatus = -1;
}
}

return IDstatus;

}
<小时/>

编辑:

有两件事:

该方法无法使用 SearchID() 方法,因为即使我现在有 2 个 ID 为 0,他们仍然接受它

否则是因为 c.ID 保持为 0。

当我输入数据时,它正在接受它,但是当我尝试输出整个记录时,客户端 ID 保持为 0。

除此之外,它还让我拥有两个 0 的 ID,所以很可能该方法不起作用......感谢到目前为止的所有帮助!

最佳答案

你错过了增加索引,当然,当你找到id时你应该退出循环:

while (index != records)
{
fread (&c,STRUCTSIZE,1,cfp);
if (c.ID == cID)
{
IDstatus = 1;
break; // <<<< otherwise IDStatus will be overwritten by next iteration
}
else
{
IDstatus = -1;
}
index++; // <<< otherwise endless loop
}

关于c - 如何用C编程检查文件中的ID是否已存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13978594/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com