//乘客节点 typedef struct CustomerNode { char name[10];//客户姓名 int clientTickets;//客户订票量 char identification[20];//客户身份证号码 int rank;//座位等级 CustomerNode *next; } CustomerNode, *CusLinkList; //候补队列中的节点 typedef struct WaitPassenger { char name[10];//姓名 char identification[20]; //身份证 int preTickets;//预定的票量 struct WaitPassenger *next; } WaitQNode, *PWait; //候补队列 typedef struct Queue { PWait front;//等候替补客户名单域的头指针 PWait rear;//等候替补客户名单域的尾指针 } LinkQueue; //封装乘客的姓名和订票量和身份证 //用于候补客户出队时把关键字返回 typedef struct NameAndNumAndID { char name[10];//姓名 char identification[20]; //身份证号码 int num;//订票量 } NameAndNumAndID; //车次节点 typedef struct Flight { char startPoint[20];//起点站名 char destination[20];//终点站名 char flightCodeID[20];//车次ID(相当于主键) char planeNum[20];//列车号 char day[20];//出发日期(星期几) int totalTickets;//乘员定额(总票数) int left;//总余票量 int leftEconomicTicket; //二等座剩余量 int leftBusinessTicket; //一等座剩余量 Flight *next; CusLinkList cusLinkList;//乘员名单域,指向乘员名单链表的头指针 LinkQueue waitQueue1;//二等座候补,等候替补的客户名单域,指向一个队列 LinkQueue waitQueue2;//一等座候补,等候替补的客户名单域,指向一个队列 } Flight, FlightNode, *PFlight; //定义全局指针变量pFlight,车次链表的头指针 Flight *pFlight; 用c语言实现该结构体的文件读写操作并给出具体代码
时间: 2023-08-10 17:07:29 浏览: 207
C/C++中的typedef和#define详解
以下是该结构体的文件读写操作的具体代码:
1. 写入文件
```c
void writeToFile() {
FILE *fp = fopen("data.txt", "wb");
if(fp == NULL) {
printf("文件打开失败!");
return;
}
Flight *currentFlight = pFlight;
while(currentFlight != NULL) {
fwrite(currentFlight, sizeof(Flight), 1, fp);
CustomerNode *currentCustomer = currentFlight->cusLinkList;
while(currentCustomer != NULL) {
fwrite(currentCustomer, sizeof(CustomerNode), 1, fp);
currentCustomer = currentCustomer->next;
}
PWait currentWait = currentFlight->waitQueue1.front;
while(currentWait != NULL) {
fwrite(currentWait, sizeof(WaitQNode), 1, fp);
currentWait = currentWait->next;
}
currentWait = currentFlight->waitQueue2.front;
while(currentWait != NULL) {
fwrite(currentWait, sizeof(WaitQNode), 1, fp);
currentWait = currentWait->next;
}
currentFlight = currentFlight->next;
}
fclose(fp);
}
```
2. 从文件中读取
```c
void readFromFile() {
FILE *fp = fopen("data.txt", "rb");
if(fp == NULL) {
printf("文件打开失败!");
return;
}
pFlight = NULL;
Flight *currentFlight = NULL;
while(!feof(fp)) {
Flight *newFlight = (Flight*)malloc(sizeof(Flight));
fread(newFlight, sizeof(Flight), 1, fp);
newFlight->cusLinkList = NULL;
newFlight->waitQueue1.front = NULL;
newFlight->waitQueue1.rear = NULL;
newFlight->waitQueue2.front = NULL;
newFlight->waitQueue2.rear = NULL;
if(pFlight == NULL) {
pFlight = newFlight;
currentFlight = newFlight;
} else {
currentFlight->next = newFlight;
currentFlight = currentFlight->next;
}
for(int i=0; i<newFlight->totalTickets; i++) {
CustomerNode *newCustomer = (CustomerNode*)malloc(sizeof(CustomerNode));
fread(newCustomer, sizeof(CustomerNode), 1, fp);
newCustomer->next = NULL;
if(newCustomer->rank == 1) {
newFlight->leftBusinessTicket--;
} else if(newCustomer->rank == 2) {
newFlight->leftEconomicTicket--;
}
if(newFlight->cusLinkList == NULL) {
newFlight->cusLinkList = newCustomer;
} else {
CustomerNode *currentCustomer = newFlight->cusLinkList;
while(currentCustomer->next != NULL) {
currentCustomer = currentCustomer->next;
}
currentCustomer->next = newCustomer;
}
}
while(1) {
WaitQNode *newWait = (WaitQNode*)malloc(sizeof(WaitQNode));
fread(newWait, sizeof(WaitQNode), 1, fp);
if(newWait->name[0] == '\0') {
free(newWait);
break;
}
newWait->next = NULL;
if(newFlight->waitQueue1.rear == NULL) {
newFlight->waitQueue1.front = newWait;
newFlight->waitQueue1.rear = newWait;
} else {
newFlight->waitQueue1.rear->next = newWait;
newFlight->waitQueue1.rear = newWait;
}
}
while(1) {
WaitQNode *newWait = (WaitQNode*)malloc(sizeof(WaitQNode));
fread(newWait, sizeof(WaitQNode), 1, fp);
if(newWait->name[0] == '\0') {
free(newWait);
break;
}
newWait->next = NULL;
if(newFlight->waitQueue2.rear == NULL) {
newFlight->waitQueue2.front = newWait;
newFlight->waitQueue2.rear = newWait;
} else {
newFlight->waitQueue2.rear->next = newWait;
newFlight->waitQueue2.rear = newWait;
}
}
}
fclose(fp);
}
```
阅读全文