You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言追加模式下如何检测文件中重复的座位号整数?

解决方案:检测已预订的座位号

要实现座位号的重复检测,你需要先读取文件中已有的所有预订记录,对比当前输入的餐厅名称和座位号是否已存在。具体步骤和代码修改如下:

核心思路

  1. 在用户输入座位号后,以读模式打开文件,遍历所有已存储的预订记录。
  2. 逐个对比每条记录的restaurantName和seatNo:如果两者都匹配,说明该座位已被预订。
  3. 根据检查结果,提示用户重新输入或继续完成预订流程。
  4. 确认座位未被占用后,再以追加模式写入新的预订记录。

修改后的代码示例

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h> // 对应getch(),Linux环境可替换为getchar()

struct restaurant
{
   int seats, seatNo, seatAmount, pizzaQt, burgerQt, drinkQt, pizzaPrice, burgerPrice, drinkPrice;
   char name[20], restaurantName[20];
   int foodTotal, total;
   int counter;
};

int book() 
{
    FILE *fp;
    struct restaurant r, existing; // existing用于存储读取到的已有记录
    char choice, choice1;
    int isBooked = 0; // 标记座位是否已被预订

    r.counter=0;

    do
    {
        printf("Proceed to booking...");
        getch();

        printf("\n\t\t\t\t---BOOK SEAT---");
        printf("\n\t\t\tEnter your name : ");
        scanf("%s", r.name);
        printf("\n\t\t\tEnter the restaurant name (ABC, XYZ, PQR, EFG) : ");
        scanf("%s", r.restaurantName);
        printf("\n\t\t\tEnter the number of seats : ");
        scanf("%d", &r.seats);
        printf("\n\t\t\tEnter the seat number(s) : ");
        scanf("%d", &r.seatNo);

        // ------------ 新增:检查座位是否已被预订 ------------
        isBooked = 0;
        // 以二进制读模式打开文件(对应后续二进制写入)
        fp = fopen("Restaurant.txt", "rb"); 
        if(fp != NULL)
        {
            // 循环读取所有记录
            while(fread(&existing, sizeof(struct restaurant), 1, fp) == 1)
            {
                // 同时匹配餐厅名称和座位号,避免不同餐厅座位号重复问题
                if(strcmp(existing.restaurantName, r.restaurantName) == 0 && existing.seatNo == r.seatNo)
                {
                    isBooked = 1;
                    break;
                }
            }
            fclose(fp); // 关闭读模式文件
        }

        if(isBooked)
        {
            printf("\n\t\t\tError: Seat %d in %s is already booked! Please choose another seat.\n", r.seatNo, r.restaurantName);
            continue; // 回到输入环节重新选择座位
        }
        // ---------------------------------------------------

        // 后续点餐、计算总价等逻辑可在此补充
        printf("\n\t\t\tSeat is available! Proceed to order...");

        // 完成输入后,以二进制追加模式写入文件
        fp = fopen("Restaurant.txt", "ab");
        if(fp == NULL)
        {
            printf("\nFailed to open the file for writing.");
            exit(1);
        }
        fwrite(&r, sizeof(struct restaurant), 1, fp);
        fclose(fp);

        printf("\n\t\t\tBooking successful!");
        printf("\n\t\t\tDo you want to book another seat? (Y/N) : ");
        scanf(" %c", &choice); // 空格跳过输入缓冲区的换行符

    } while(choice == 'Y' || choice == 'y');

    return 0;
}

关键细节说明

  • 文件模式选择:使用二进制模式("rb"/"ab")读写结构体,避免文本模式下的格式错乱问题。如果之前用文本模式写入,需调整读取逻辑(比如用fscanf逐个读取字段)。
  • 多座位支持:当前代码仅处理单个座位号,若要支持多座位预订,需修改结构体(比如添加座位号数组),并在检查时遍历所有输入的座位号逐一验证。
  • 输入缓冲区处理:读取字符类型时,scanf(" %c", &choice)中的空格可跳过之前输入残留的换行符,避免误判。

内容的提问来源于stack exchange,提问作者user18657113

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 12:57:12