gpt4 book ai didi

c - 如何让案例2记住案例1在文件中保存了什么?

转载 作者:行者123 更新时间:2023-11-30 17:38:30 25 4
gpt4 key购买 nike

我已经制作了自己的程序。

案例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。我现在该怎么办?

最佳答案

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

可能的问题可能如下

在程序的运行状态期间,您已为输入总线信息的总线编号设置了状态 1,现在在预订时,您检查该总线状态,如果设置为 1,则进行预订。因此,此逻辑将一直有效,直到您的程序加载在 RAM 中,但它关闭并重新运行,然后 bus_inumber[] 再次初始化(可能为零)。

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

还发布更多代码,以便我们了解想法。这并不像您到目前为止所做的那么简单,因为程序可能与巴士预订系统或其他东西有关。它需要大量验证和错误检查机制。

关于c - 如何让案例2记住案例1在文件中保存了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22112754/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com