//乘客节点 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 CusLinkList2{ CusLinkList head;//头指针 int count;//链表长度 } CusLinkList2; //候补队列 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;用c语言实现该结构体的文件读取操作,并写出具体代码
时间: 2023-08-28 16:05:19 浏览: 93
假设我们需要从一个名为"flight.txt"的文件中读取Flight结构体的信息,以下是具体的代码实现:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LEN 100 //最大行长
//乘客节点
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 CusLinkList2{
CusLinkList head;//头指针
int count;//链表长度
} CusLinkList2;
//候补队列
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;
//从文件中读取一行数据
char* readLine(FILE* fp) {
char* line = (char*)malloc(MAX_LEN * sizeof(char));
if (!line) {
perror("Memory allocation failed!");
exit(1);
}
if (fgets(line, MAX_LEN, fp) == NULL) {
free(line);
return NULL;
}
int len = strlen(line);
if (len > 0 && line[len - 1] == '\n') {
line[len - 1] = '\0';
}
return line;
}
//从文件中读取一个Flight结构体的信息
PFlight readFlightFromFile(FILE* fp) {
char* line;
PFlight flight = (PFlight)malloc(sizeof(FlightNode));
if (!flight) {
perror("Memory allocation for flight failed!");
exit(1);
}
flight->cusLinkList.head = NULL;
flight->waitQueue1.front = flight->waitQueue2.front = NULL;
flight->waitQueue1.rear = flight->waitQueue2.rear = NULL;
flight->next = NULL;
while ((line = readLine(fp)) != NULL) {
if (strncmp(line, "startPoint:", 11) == 0) {
strcpy(flight->startPoint, line + 11);
}
else if (strncmp(line, "destination:", 12) == 0) {
strcpy(flight->destination, line + 12);
}
else if (strncmp(line, "flightCodeID:", 13) == 0) {
strcpy(flight->flightCodeID, line + 13);
}
else if (strncmp(line, "planeNum:", 9) == 0) {
strcpy(flight->planeNum, line + 9);
}
else if (strncmp(line, "day:", 4) == 0) {
strcpy(flight->day, line + 4);
}
else if (strncmp(line, "totalTickets:", 13) == 0) {
flight->totalTickets = atoi(line + 13);
flight->left = flight->totalTickets;
}
else if (strncmp(line, "left:", 5) == 0) {
flight->left = atoi(line + 5);
}
else if (strncmp(line, "leftEconomicTicket:", 19) == 0) {
flight->leftEconomicTicket = atoi(line + 19);
}
else if (strncmp(line, "leftBusinessTicket:", 19) == 0) {
flight->leftBusinessTicket = atoi(line + 19);
}
else if (strncmp(line, "cusLinkList:", 12) == 0) {
//读取乘员名单域
CusLinkList cusList = &flight->cusLinkList;
cusList->count = 0;
while ((line = readLine(fp)) != NULL && strncmp(line, "waitQueue1:", 11) != 0 && strncmp(line, "waitQueue2:", 11) != 0) {
CustomerNode* cus = (CustomerNode*)malloc(sizeof(CustomerNode));
if (!cus) {
perror("Memory allocation for customer failed!");
exit(1);
}
sscanf(line, "%s %d %s %d", cus->name, &cus->clientTickets, cus->identification, &cus->rank);
cus->next = cusList->head;
cusList->head = cus;
cusList->count++;
}
if (line != NULL && strncmp(line, "waitQueue1:", 11) == 0) {
//读取二等座候补队列
LinkQueue* queue = &flight->waitQueue1;
while ((line = readLine(fp)) != NULL && strncmp(line, "waitQueue2:", 11) != 0) {
PWait waitNode = (PWait)malloc(sizeof(WaitQNode));
if (!waitNode) {
perror("Memory allocation for wait passenger failed!");
exit(1);
}
sscanf(line, "%s %s %d", waitNode->name, waitNode->identification, &waitNode->preTickets);
waitNode->next = NULL;
if (queue->front == NULL) {
queue->front = waitNode;
}
else {
queue->rear->next = waitNode;
}
queue->rear = waitNode;
}
}
if (line != NULL && strncmp(line, "waitQueue2:", 11) == 0) {
//读取一等座候补队列
LinkQueue* queue = &flight->waitQueue2;
while ((line = readLine(fp)) != NULL) {
PWait waitNode = (PWait)malloc(sizeof(WaitQNode));
if (!waitNode) {
perror("Memory allocation for wait passenger failed!");
exit(1);
}
sscanf(line, "%s %s %d", waitNode->name, waitNode->identification, &waitNode->preTickets);
waitNode->next = NULL;
if (queue->front == NULL) {
queue->front = waitNode;
}
else {
queue->rear->next = waitNode;
}
queue->rear = waitNode;
}
}
}
}
return flight;
}
//从文件中读取多个Flight结构体的信息
PFlight readFlightsFromFile(char* fileName) {
FILE* fp = fopen(fileName, "r");
if (!fp) {
perror("Failed to open file!");
exit(1);
}
PFlight head = NULL;
PFlight tail = NULL;
while (!feof(fp)) {
PFlight flight = readFlightFromFile(fp);
if (flight != NULL) {
if (head == NULL) {
head = flight;
}
else {
tail->next = flight;
}
tail = flight;
}
}
fclose(fp);
return head;
}
int main() {
PFlight flights = readFlightsFromFile("flight.txt");
//对读取到的Flight结构体进行操作
//...
return 0;
}
```
阅读全文