//乘客节点 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; //初始化车次链表pFlight = (Flight *) malloc(sizeof(Flight));//申请头结点的空间if (pFlight == NULL) exit(0);pFlight->next = NULL; 用c语言实现该结构体的文件读写操作并给出具体代码
时间: 2023-08-10 09:07:28 浏览: 91
C/C++中的typedef和#define详解
以下是实现该结构体的文件读写操作的具体代码:
// 写入车次信息到文件中
void writeFlightToFile(Flight* pFlight, char* fileName) {
FILE* fp;
if ((fp = fopen(fileName, "wb")) == NULL) {
printf("Cannot open file.\n");
return;
}
fwrite(pFlight, sizeof(Flight), 1, fp);
Flight* p = pFlight->next;
while (p != NULL) {
fwrite(p, sizeof(Flight), 1, fp);
p = p->next;
}
fclose(fp);
}
// 从文件中读取车次信息
Flight* readFlightFromFile(char* fileName) {
FILE* fp;
if ((fp = fopen(fileName, "rb")) == NULL) {
printf("Cannot open file.\n");
return NULL;
}
Flight* pFlight = (Flight*)malloc(sizeof(Flight));
if (pFlight == NULL) {
printf("Memory allocation failed.\n");
return NULL;
}
fread(pFlight, sizeof(Flight), 1, fp);
Flight* p = pFlight;
while (!feof(fp)) {
Flight* q = (Flight*)malloc(sizeof(Flight));
if (q == NULL) {
printf("Memory allocation failed.\n");
return NULL;
}
fread(q, sizeof(Flight), 1, fp);
p->next = q;
p = q;
}
p->next = NULL;
fclose(fp);
return pFlight;
}
// 写入乘员信息到文件中
void writeCustomerToFile(CusLinkList cusLinkList, char* fileName) {
FILE* fp;
if ((fp = fopen(fileName, "wb")) == NULL) {
printf("Cannot open file.\n");
return;
}
CustomerNode* p = cusLinkList->next;
while (p != NULL) {
fwrite(p, sizeof(CustomerNode), 1, fp);
p = p->next;
}
fclose(fp);
}
// 从文件中读取乘员信息
CusLinkList readCustomerFromFile(char* fileName) {
FILE* fp;
if ((fp = fopen(fileName, "rb")) == NULL) {
printf("Cannot open file.\n");
return NULL;
}
CusLinkList cusLinkList = (CusLinkList)malloc(sizeof(CustomerNode));
if (cusLinkList == NULL) {
printf("Memory allocation failed.\n");
return NULL;
}
cusLinkList->next = NULL;
CustomerNode* p = cusLinkList;
while (!feof(fp)) {
CustomerNode* q = (CustomerNode*)malloc(sizeof(CustomerNode));
if (q == NULL) {
printf("Memory allocation failed.\n");
return NULL;
}
fread(q, sizeof(CustomerNode), 1, fp);
p->next = q;
p = q;
}
p->next = NULL;
fclose(fp);
return cusLinkList;
}
// 写入候补客户信息到文件中
void writeWaitPassengerToFile(LinkQueue waitQueue, char* fileName) {
FILE* fp;
if ((fp = fopen(fileName, "wb")) == NULL) {
printf("Cannot open file.\n");
return;
}
PWait p = waitQueue.front;
while (p != NULL) {
fwrite(p, sizeof(WaitQNode), 1, fp);
p = p->next;
}
fclose(fp);
}
// 从文件中读取候补客户信息
LinkQueue readWaitPassengerFromFile(char* fileName) {
FILE* fp;
if ((fp = fopen(fileName, "rb")) == NULL) {
printf("Cannot open file.\n");
LinkQueue queue = { NULL, NULL };
return queue;
}
LinkQueue queue = { NULL, NULL };
PWait p = NULL;
while (!feof(fp)) {
PWait q = (PWait)malloc(sizeof(WaitQNode));
if (q == NULL) {
printf("Memory allocation failed.\n");
return queue;
}
fread(q, sizeof(WaitQNode), 1, fp);
q->next = NULL;
if (queue.front == NULL) {
queue.front = q;
p = q;
}
else {
p->next = q;
p = q;
}
}
fclose(fp);
queue.rear = p;
return queue;
}
阅读全文