我有一个文件 car.txt 和一个文件 reservation.txt。这两个文件都有一个保留号 (Rxxx)。
我想使用reservation.txt中的预订号来打印带有相应预订号的汽车的详细信息。或者更准确地说,我试图找到在任何给定日期可用的所有汽车的详细信息。
car.txt:(从让到右:reservationID、carID、carYOM、carMake、carModel、carFuel、category)
R002;V001;2003;Toyota;Camry;Petrol;Budget
R007;V002;2005;Toyota;Prius;Petrol;Economy
R001;V003;1999;Ford;Falcon;Petrol;Midsize
R008;V004;2007;Ford;Territory;Diesel;Fullsize
R011;V005;2010;Ferrari;599;Petrol;Fullsize
R035;V006;1998;Holden;Comadore;Diesel;Midsize
R006;V007;2008;Honda;Civic;Petrol;Budget
R004;V008;2000;Mazda;MX5;Petrol;Economy
reservation.txt:(从左到右:reservationID、customerID、reservationStartDate、reservationStartTime、reservationEndDate、reservationEndTime)
R001;C005;12/02/2012;09:15A.M;15/03/2012;05:00P.M
R002;C002;15/04/2012;10:00A.M;22/04/2012;10:30A.M
R003;C003;16/01/2012;02:11P.M;15/04/2012;12:00P.M
R004;C004;05/05/2012;03:00P.M;08/05/2012;10:40A.M
R005;C005;15/04/2012;10:00A.M;23/04/2012;05:00P.M
R006;C006;11/04/2012;05:30P.M;15/04/2012;10:00A.M
R010;C008;15/05/2012;03:15P.M;18/05/2012;11:00A.M
R011;C007;15/04/2012;11:40P.A;23/04/2012;09:00A.M
如果我输入任何日期,它只会决定输入的日期是否在预订开始日期和结束日期之间。“所有汽车都可用”。
但是,如果我输入 13/02/2012,它会打印 7 次“cars.txt 中没有匹配的 resID”。
有问题的代码:
#include <stdio.h>
#include <string.h>
#define MAX_CAR 100
#define MAX_RES 100
int main(){
typedef struct{ //car struct
char reservationID[20];
char carID[20];
char carYOM[20];
char carMake[20];
char carModel[50];
char carFuel[20];
char catagory[20];
} car_t;
typedef struct{ //res struct
char reservationID[20];
char customerID[20];
char reservationStartDate[20];
char reservationStartTime[20];
char reservationEndDate[50];
char reservationEndTime[20];
} res_t;
car_t car[MAX_CAR]; //car array
res_t reservation[MAX_RES]; //res array
FILE *carHandle;
FILE *resHandle;
char line[100];
char *item;
int rescount = 0;
int carcount =0;
int k;
int i;
int option;
char choice[20];
resHandle = fopen("reservation.txt","r");
while (fgets(line, 99, resHandle)){ //cut up the reservation file line by line and put the bits into the res array.
item = strtok(line,";");
strcpy(reservation[rescount].reservationID,item);
item = strtok(NULL,";");
strcpy(reservation[rescount].customerID,item);
item = strtok(NULL,";");
strcpy(reservation[rescount].reservationStartDate,item);
item = strtok(NULL,";");
strcpy(reservation[rescount].reservationStartTime,item);
item = strtok(NULL,";");
strcpy(reservation[rescount].reservationEndDate,item);
item = strtok(NULL,"\n");
strcpy(reservation[rescount].reservationEndTime,item);
rescount++;
}
fclose(resHandle);
carHandle = fopen("car.txt","r");
while (fgets(line, 99, carHandle)){ //cut up the car file line by line and put the bits into the car array.
item = strtok(line,";");
strcpy(car[carcount].reservationID,item);
item = strtok(NULL,";");
strcpy(car[carcount].carID,item);
item = strtok(NULL,";");
strcpy(car[carcount].carYOM,item);
item = strtok(NULL,";");
strcpy(car[carcount].carMake,item);
item = strtok(NULL,";");
strcpy(car[carcount].carModel,item);
item = strtok(NULL,";");
strcpy(car[carcount].carFuel,item);
item = strtok(NULL,"\n");
strcpy(car[carcount].catagory,item);
carcount++;
}
fclose(carHandle);
printf("Enter todays date:");
scanf("%s", choice);
for (k=0;k<=rescount; k++){
if (strcmp(choice,reservation[k].reservationEndDate)<0 && strcmp(choice,reservation[k].reservationStartDate)>0){
for (i=0;i<=carcount; i++){
if (strcmp(car[k].reservationID,reservation[i].reservationID)==0){
printf("\nreservationID: %s\nreservationStartTime: %s\ncustomerID: %s\ncarid: %s\nyom: %s\nmake: %s\nmodel: %s\nfueltype: %s\ncategory: %s\n\n", car[k].reservationID, reservation[i].reservationStartTime, reservation[i].customerID, car[k].carID, car[k].carYOM, car[k].carMake, car[k].carModel, car[k].carFuel, car[k].catagory);
//printf("This works");
goto outofloop;
}else printf("\n\nno matching resID in cars.txt\n");
}
}
else printf("\nall the cars are available\n");
break;
}
outofloop:
return(0);
}
任何帮助,将不胜感激。:)
编辑:更新的代码。
这是输出,仍然错误:(:
输入今天日期:13/02/2012
car.txt 中没有匹配的 resID
reservationID: R002
reservationStartTime: 10:00A.M
customerID: C002
carid: V001
yom: 2003
make: Toyota
model: Camry
fueltype: Petrol
category: Budget
Press any key to continue . . .
输入今天日期:13/02/2012
所有的车都可用
按任意键继续...