c - 它只显示一个酒店预订
问题描述
我正在预订酒店,所以我尝试使用用户插入的 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);
}
解决方案
你的 count 和 rec 是未定义的,因为它们没有初始化。
在比较之前请先阅读 Id。
fread(&temp->id, sizeof(int), 1, fp);
printf("\niD: %d\n", temp->id);
if (choice == temp->id) {
}
推荐阅读
- vue.js - 通过 URL 或 F5 调用时,Vue.js 组件不加载/渲染数据
- ios - 只允许在 Firebase 中注册单个域 (@example.com) 电子邮件不起作用
- node.js - 从反应原生应用程序请求 POST 返回未定义的响应
- .net - 是否可以在 .NET 代码中找到所有具体类型的泛型方法调用?
- ansible - Ansible Script 模块如何返回脚本中每个命令的执行结果?
- c++ - 检查 std::any 变量是否包含 std::string 时出现问题
- karate - 如何根据响应进行有条件的空手道中止
- javascript - 在 await 完成执行之前函数返回
- sql - 从R中保存在.sql文件中的sql数据库导入数据
- wpf - 如何在 WPF 中按字节 [] 更新 WritableBitmap