如何使案例2记住什么案例1保存在文件中?

时间:2014-03-01 09:51:40

标签: c

我制作了自己的节目。

案例1:

            Records[0] = fopen("Records.txt", "r+"); // read mode
            Records[1] = fopen("Records1.txt", "r+");
            Records[2] = fopen("Records2.txt", "r+");
            Records[3] = fopen("Records3.txt", "r+");
            Records[4] = fopen("Records4.txt", "r+");
            Records[5] = fopen("Records5.txt", "r+");
            Records[6] = fopen("Records6.txt", "r+");
            Records[7] = fopen("Records7.txt", "r+");
            Records[8] = fopen("Records8.txt", "r+");
            Records[9] = fopen("Records9.txt", "r+");
            Records[10] = fopen("Records10.txt", "r+");

        printf("\n\t\tEnter bus no: ");
        scanf("%d", &bus);  

        if(bus_inumber[bus]==0)
            {
            printf("\t\tEnter Driver's name: ");
        scanf("%s", name[bus]);
        printf("\t\tArrival time: ");
        scanf("%d", &arrival[bus]);
            printf("\t\tDeparture: ");
        scanf("%d", &departure[bus]);
         printf("\t\tFrom: ");
        scanf("%s", from[bus]);
        printf("\t\tTo: ");
        scanf("%s", to[bus]);
        input=1;
        bus_inumber[bus]=1;

        fprintf(Records[bus], "\nBus no:\t%d \nDriver:\t%s \tArrival Time:\t%d \tDeparture Time:\t%d \nFrom:\t%s\t \tTo:\t%s\n", bus, name[bus], arrival[bus], departure[bus], from[bus], to[bus]);
        fclose(Records[bus]);

            }
        else
        {
                        printf("\t\t\n\t\t||||||This bus number already registered by %s||||||\n", name[bus]); 

        }


        }break;

我使用案例1(上面)将我的总线保存到文件中。

案例2:

            Records[0] = fopen("Records.txt", "r"); // read mode
            Records[1] = fopen("Records1.txt", "r");
            Records[2] = fopen("Records2.txt", "r");
            Records[3] = fopen("Records3.txt", "r");
            Records[4] = fopen("Records4.txt", "r");
            Records[5] = fopen("Records5.txt", "r");
            Records[6] = fopen("Records6.txt", "r");
            Records[7] = fopen("Records7.txt", "r");
            Records[8] = fopen("Records8.txt", "r");
            Records[9] = fopen("Records9.txt", "r");
            Records[10] = fopen("Records10.txt", "r");

            Reservation[0] = fopen("Reservation.txt", "r+"); // read mode
            Reservation[1] = fopen("Reservation1.txt", "r+");
            Reservation[2] = fopen("Reservation2.txt", "r+");
            Reservation[3] = fopen("Reservation3.txt", "r+");
            Reservation[4] = fopen("Reservation4.txt", "r+");
            Reservation[5] = fopen("Reservation5.txt", "r+");
            Reservation[6] = fopen("Reservation6.txt", "r+");
            Reservation[7] = fopen("Reservation7.txt", "r+");
            Reservation[8] = fopen("Reservation8.txt", "r+");
            Reservation[9] = fopen("Reservation9.txt", "r+");
            Reservation[10] = fopen("Reservation10.txt", "r+");

        printf("\t\tBus no: ");
        scanf("%d", &bus);


        if(bus_inumber[bus]==1)
        {
        printf("\t\tSeat number: ");
        scanf("%d", &seat);
        while(bus_seat[bus][seat]==1)
                {
                    printf("\t\tThis seat number has already been registered\n\t\tPlease choose another seat number:");
                    scanf("%d", &seat);
                }
        bus_seat[bus][seat]=1;
        printf("\t\tEnter passenger's name: ");
        scanf("%s", pname[bus][seat]);

        for(seat=1;seat<31;seat++)
        fprintf(Reservation[bus], "\n%d \t%s" ,seat,pname[bus][seat]  );
        fclose(Reservation[bus]);           
        }

        else
        printf("\t\tSorry, this bus is not operating yet.\n\t\tPlease install the bus to register.\n");

        }break;

我使用案例2(上面)为我的公共汽车座位做预订。

我的问题是,当我关闭程序并再次打开它时,案例2不能记住我提前安装的总线。我选择案例2并选择公共汽车号码:1它说公共汽车尚未运行。但在我选择案例2之前,我只安装了第1号巴士。我现在要做什么。?

1 个答案:

答案 0 :(得分:0)

您可能未在此处发布完整代码,但在此处查看一些代码,可能是本地或全局的一个数组bus_inumber[]可能是您问题的关键因素。

可能的问题可能如下所示

在你的程序的运行状态期间,你已经为你输入总线信息的总线号设置了状态1,现在在预订时你检查总线状态是否设置为1然后进行预约。所以这个逻辑有效,直到程序加载在RAM中它关闭并重新运行,然后bus_inumber[]再次初始化(可能为零)。

现在您需要将bus_inumber[]保存在某个文件中并在运行程序中使用文件数据初始化此数组。

同时发布更多代码,以便我们可以了解它。并且它并不简单,因为你到目前为止程序可能与bus reservation system或其他东西有关。它需要大量的验证和错误检查机制。