我正在做酒店预订,所以我试图使用用户插入的iD显示预订,但我无法获取它,程序冻结,没有任何错误或任何东西,我尝试了很多方法,在网上寻找都找不到答案。我试着在if循环中做了一次while,它也不能工作,我找不出问题所在。谢谢。
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);
}
https://stackoverflow.com/questions/56248025
复制相似问题