我正在预订酒店,所以我尝试使用用户输入的 iD 来显示预订,但我无法得到它,程序只是卡住而没有错误,或者我尝试了很多东西并在网上查找的任何东西找不到答案。我试着在 if 循环中做了一段时间它也没有工作无法弄清楚问题出在哪里。谢谢。
void display(char *a)
{
FILE *fp;
rec = count;
int choice;
fp = fopen(a ,"r");
struct hotelStruct *temp = (struct hotelStruct *)malloc(sizeof(struct hotelStruct));
temp->first_name = (char *)malloc(10* sizeof(char));
temp->last_name = (char *)malloc(15*sizeof(char));
temp->passport = (char *)malloc(15*sizeof(char));
temp->nationality = (char *)malloc(30*sizeof(char));
temp->room = (char *)malloc(10*sizeof(char));
temp->email = (char *)malloc(30*sizeof(char));
if (fp == NULL)
printf("Error!!");
printf("\nEnter your Reservations iD:\n");
scanf("%d", &choice);
fseek(fp, 0, 0);
while (rec)
{
if (choice == temp->id) {
fread(&temp->id, sizeof(int), 1, fp);
printf("\niD: %d\n", temp->id);
fread(temp->first_name, 10, 1, fp);
printf("First Name: %s\n", temp->first_name);
fread(temp->last_name, 15, 1, fp);
printf("Last name: %s\n", temp->last_name);
fread(temp->passport, 15, 1, fp);
printf("Passport: %s\n", temp->passport);
fread(temp->nationality, 30, 1, fp);
printf("Nationality: %s\n", temp->nationality);
fread(temp->room, 10, 1, fp);
printf("Room: %s\n", temp->room);
fread(&temp->bed, sizeof(int), 1, fp);
printf("Beds: %d\n", temp->bed);
fread(temp->email, 30, 1, fp);
printf("Email: %s\n", temp->email);
fread(&temp->phone_number, sizeof(int), 1, fp);
printf("Phone number: %d\n\n\n", temp->phone_number);
}
rec--;
}
fclose(fp);
free(temp);
free(temp->first_name);
free(temp->last_name);
free(temp->passport);
free(temp->room);
free(temp->email);
free(temp->nationality);
}
您的计数和记录未定义,因为它们未初始化。
在比较之前先读取 Id。
fread(&temp->id, sizeof(int), 1, fp);
printf("\niD: %d\n", temp->id);
if (choice == temp->id) {
}
我是一名优秀的程序员,十分优秀!