⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 train_booking_fread.c

📁 火车定标系统...用VC编译通过的
💻 C
📖 第 1 页 / 共 2 页
字号:
	create_iii_dir(filepath, TRAIN_INFO_DIR, sq_id, 
		time_id, SB_INFO_FILE);				// 生成文件路径

	// 检测文件有效性
	if (!check_data(filepath, 1))
	{
		message(ERROR, "从文件加载列车软卧车厢信息失败!", WC);

		return FAIL;
	}

	fp = fopen(filepath, "rb");

	// 读取信息到顺序表中
	fread(sbed_arr, sizeof(sbed_info), amount, fp);

	// 检测车厢与列车的从属关系
	if ((strcpy(sq_id, sbed_arr->master)) || 
		(strcpy(time_id, sbed_arr->time_id)))
	{
		message(ERROR, "加载的车厢信息不属于本列车!", NULL);
		message(WARN, "加载车厢信息完全失败!", WC);

		return FAIL;
	}
	fclose(fp);

	return OK;
}



/////////////////////////////////////////////////////////////////////////////
// 函数功能:	从文件加载列车硬卧信息数组
//
// 函数参数:	hbed_arr:硬卧信息结构数组首地址
//				sq_id:车次编号
//				timd_id:该趟列车的时间标识符
//				amonut:硬卧车厢数量
//
/////////////////////////////////////////////////////////////////////////////
status load_hbed_info_list(hbed_info *hbed_arr, char *sq_id, 
						   char *time_id, int amount)
{
	char filepath[MAX_FILE_PATH_LEN];
	FILE *fp;

	printf("【信息】正在加载列车(%s)的硬卧车厢信息...\n", sq_id);

	create_iii_dir(filepath, TRAIN_INFO_DIR, sq_id, 
		time_id, HB_INFO_FILE);				// 生成文件路径

	// 检测文件有效性
	if (!check_data(filepath, 1))
	{
		message(ERROR, "从文件加载列车硬卧车厢信息失败!", WC);

		return FAIL;
	}

	fp = fopen(filepath, "rb");

	// 检测车厢与列车的从属关系
	if ((strcmp(sq_id, hbed_arr->master)) || 
		(strcmp(time_id, hbed_arr->time_id)))
	{
		message(ERROR, "加载的车厢信息不属于本列车!", NULL);
		message(WARN, "加载车厢信息完全失败!", WC);

		return FAIL;
	}

	// 读取信息到顺序表中
	fread(hbed_arr, sizeof(hbed_info), amount, fp);
	fclose(fp);

	return OK;
}



/////////////////////////////////////////////////////////////////////////////
// 函数功能:	从文件加载列车硬座信息数组
//
// 函数参数:	hseat_arr:软卧信息结构数组首地址
//				sq_id:车次编号
//				timd_id:该趟列车的时间标识符
//				amonut:硬座车厢数量
//
/////////////////////////////////////////////////////////////////////////////
status load_hseat_info_list(hseat_info *hseat_arr, char *sq_id, 
						   char *time_id, int amount)
{
	char filepath[MAX_FILE_PATH_LEN];
	FILE *fp;

	printf("【信息】正在加载列车(%s)的硬座车厢信息...\n", sq_id);

	create_iii_dir(filepath, TRAIN_INFO_DIR, sq_id, 
		time_id, HS_INFO_FILE);				// 生成文件路径

	// 检测文件有效性
	if (!check_data(filepath, 1))
	{
		message(ERROR, "从文件加载列车硬座车厢信息失败!", WC);

		return FAIL;
	}

	fp = fopen(filepath, "rb");

	// 检测车厢与列车的从属关系
	if ((strcmp(sq_id, hseat_arr->master)) || 
		(strcmp(time_id, hseat_arr->time_id)))
	{
		message(ERROR, "加载的车厢信息不属于本列车!", NULL);
		message(WARN, "加载车厢信息完全失败!", WC);

		return FAIL;
	}

	// 读取信息到顺序表中
	fread(hseat_arr, sizeof(hseat_info), amount, fp);
	fclose(fp);

	return OK;
}



/////////////////////////////////////////////////////////////////////////////
// 函数功能:	从文件加载所有座位信息
//
// 函数参数:	sbed_arr:软卧信息结构数组首地址
//				hbed_arr:硬卧信息结构数组首地址
//				hseat_arr:硬座信息结构数组首地址
//				sq_id:列车对应车次编号
//				time_id:列车时间标识符
//				sbed:软卧车厢数量
//				hbed:硬卧车厢数量
//				hseat:硬座车厢数量
//
/////////////////////////////////////////////////////////////////////////////
status load_dot_info_list(sbed_info *sbed_arr, hbed_info *hbed_arr, 
						  hseat_info *hseat_arr, char *sq_id, char time_id,
						  int sbed, int hbed, int hseat)
{
	load_sbed_info_list(sbed_arr, sq_id, time_id, sbed);
	load_hbed_info_list(hbed_arr, sq_id, time_id, hbed);
	load_hseat_info_list(hseat_arr, sq_id, time_id, hseat);

	return OK;
}




/////////////////////////////////////////////////////////////////////////////
// 函数功能:	生成默认的管理员信息
//
// 函数参数:	psngr_info_head:默认管理员信息结构体指针
//				system_info_pt:指向系统配置信息体的指针
//
/////////////////////////////////////////////////////////////////////////////
status create_default_admin_info(passenger_info *psngr_info_pt,
								 system_info *system_info_pt)
{
	message(WARN, "正在重置默认管理员信息...", NULL);

	build_ii_dir(USER_INFO_DIR, "陆遥", NULL);
	prepare_psngr_dir("陆遥");

	strcpy(psngr_info_pt->passenger_id, "陆遥");		// 设置默认管理员名
	strcpy(psngr_info_pt->password, "123");
	strcpy(psngr_info_pt->station_id, "武汉");
	psngr_info_pt->type = 0;

	psngr_info_pt->total_booked_tickets = 0;
	psngr_info_pt->total_booked_train = 0;
	psngr_info_pt->total_cost = 0;
	psngr_info_pt->sysmsg_amount = 0;
	psngr_info_pt->sysmsg_new = 0;

	psngr_info_pt->booked_links.next = NULL;
	psngr_info_pt->sysmsg_links.next = NULL;

	// 注册索引
	register_psngr_info(system_info_pt, psngr_info_pt->passenger_id, 1);

	// 将创建信息写入文件
	if (save_psngr_info(psngr_info_pt))
	{
		message(INFO, "成功创建默认管理员信息!", NULL);
	}
	else
	{
		message(ERROR, "创建默认管理员信息时存储文件出错!", NULL);
		message(WARN, "创建默认管理员信息失败!", WC);

		return FAIL;
	}
}



/////////////////////////////////////////////////////////////////////////////
// 函数功能:	从文件加载已注册用户列表
//
// 函数参数:	user_item_head:已注册用户链表头结点指针
//				station_id:已注册用户所属车站名
//				amount:已注册用户数量
//
/////////////////////////////////////////////////////////////////////////////	
status load_psngr_item_list(user_item *user_item_head, char *station_id, int amount)
{
	int i;
	FILE *fp;
	user_item *user_item_pt;
	char filepath[MAX_FILE_PATH_LEN];

	printf("【信息】正在加载车站(%s)的所有用户列表...\n", station_id);

	// 生成文件路径
	create_i_dir(filepath, SYS_INFO_DIR, PSNGR_ITEM_FILE);

	// 检测文件有效性
	if (!check_data(filepath, 1))
	{
		message(ERROR, "从文件加载用户列表失败!", WC);

		return FAIL;
	}
		
	fp = fopen(filepath, "rb");

	for (i = 0; i < amount; i++)
	{
		// 从文件中读取用户链表信息
		if (!(user_item_pt = (user_item *)malloc(sizeof(user_item))))
		{
			message(ERROR, "加载用户列表时读申请内存失败!", NULL);
			message(WARN, "加载用户列表失败!", WC);

			free_user_item_info_list(user_item_head);		// 释放无效内存

			return FAIL;
		}

		fread(user_item_pt, sizeof(user_item), 1, fp);

		// 检测用户与车站的从属关系
		if (strcmp(station_id, user_item_pt->master))
		{
			message(ERROR, "加载的用户信息不属于本车站!", NULL);
			message(WARN, "加载用户列表信息完全失败!", WC);// 断点

			// 释放无效内存
			free_user_item_info_list(user_item_head);
			free(user_item_pt);

			return FAIL; 
		}

		user_item_head->next = user_item_pt;
		user_item_head = user_item_head->next ;
	}
	
	if (amount)			// 如果加载的索引数量不为0则要封尾
	{
		user_item_pt->next = NULL;
	}
	fclose(fp);

	return OK;
}



/////////////////////////////////////////////////////////////////////////////
// 函数功能:	从文件加载前台用户信息链
//
// 函数参数:	psngr_info_head:前台用户链表头结点指针,用于返回加载数据
//				station_id:前台用户所属车站名
//				user_item_head:用户信息索引链表头结点指针
//				amount:前台用户数量
//
/////////////////////////////////////////////////////////////////////////////	
status load_psngr_info_list(passenger_info *psngr_info_head, char *station_id,
							user_item *user_item_head, int amount)
{
	FILE *fp;
	char filepath[MAX_FILE_PATH_LEN];
	passenger_info *psngr_info_pt;
	int i, amt = 0;

	printf("【信息】正在加载站点(%s)的前台用户信息...\n", station_id);

	if (!(user_item_head->next))
	{
		message(WARN, "乘客数据索引为空!没有乘客数据可加载。", W);

		return FAIL;
	}

	while (user_item_head->next)		// 根据索引来载入信息
	{
		user_item_head = user_item_head->next;
		// 生成文件路径
		create_ii_dir(filepath, USER_INFO_DIR, user_item_head->psngr, PSNGR_INFO_FILE);

		// 检测文件有效性
		if (!check_data(filepath, 1))
		{
			message(ERROR, "从文件加载前台用户信息失败!", WC);

			return FAIL;
		}

		// 只读二进制方式打开文件读取信息
		fp = fopen(filepath, "rb");

		if (!(psngr_info_pt = (passenger_info *)malloc(sizeof(passenger_info))))
		{
			message(ERROR, "加载前台用户信息时申请内存失败!", NULL);
			message(ERROR, "加载前台用户信息失败!", WC);

			// 释放无效内存空间
			free_passenger_info_list(psngr_info_head);

			return FAIL;
		}

		fread(psngr_info_pt, sizeof(passenger_info), 1, fp);

		// 检测用户与车站的从属关系
		if (strcmp(station_id, psngr_info_pt->station_id))
		{
			printf("【错误】加载的前台用户(%s)信息不属于本车站!\n", psngr_info_pt->passenger_id);

#ifdef _DEBUG_
			printf("车站名:%s\n", station_id);
			printf("乘客名:%s\n", psngr_info_head->passenger_id); 
			printf("乘客注册车站名:%s\n", psngr_info_head->station_id);
#endif

			message(WARN, "加载前台用户信息完全失败!", WC);

			// 释放无效内存空间
			free_passenger_info_list(psngr_info_head);
			free(psngr_info_pt);

			return FAIL;
		}

		// 加载用户的系统消息链表
		load_sysmsg_list(&psngr_info_pt->sysmsg_links,
			psngr_info_pt->passenger_id,
			psngr_info_pt->sysmsg_amount);

		// 加载用户已订票信息链表
		load_booked_info_list(&psngr_info_pt->booked_links, 
			psngr_info_pt->passenger_id, 
			psngr_info_pt->total_booked_train);

		// 链上前台用户信息
		psngr_info_head->next = psngr_info_pt;
		psngr_info_head = psngr_info_head->next;

		amt++;		// 检测计数器自加
	}
	psngr_info_pt->next = NULL;		// 封尾

	if (amt != amount)
	{
		message(WARN, "加载的乘客个数与系统配置中的乘客个数不一致,可能存在数据丢失问题。", W);
	}

	fclose(fp);

	return OK;
}

/////////////////////////////////////////////////////////////////////////////
// 函数功能:	从文件加载用户订票信息
//
// 函数参数:	booked_info_head:用户订票信息链表头结点指针
//				psngr_id:用户名
//				amount:总共订了几次列车的票(每次列车的每趟列车为一个结点)
//
/////////////////////////////////////////////////////////////////////////////	
status load_booked_info_list(booked_info *booked_info_head, char *psngr_id,
							 int amount)
{
	FILE *fp;
	char filepath[MAX_FILE_PATH_LEN];
	int i;
	booked_info *booked_info_pt;

	printf("【信息】正在加载用户(%s)的订票信息...\n", psngr_id);

	// 生成文件路径
	create_ii_dir(filepath, USER_INFO_DIR, psngr_id, BOOKED_INFO_FILE);

	// 检测文件有效性
	if (!check_data(filepath, 1))
	{
		message(ERROR, "从文件加载用户订票信息失败!", WC);

		return FAIL;
	}

	// 只读二进制方式打开文件读取信息
	fp = fopen(filepath, "rb");

	for (i = 0; i < amount; i++)
	{
		if (!(booked_info_pt = (booked_info *)malloc(sizeof(booked_info))))
		{
			message(ERROR, "加载用户订票信息时申请内存失败!", NULL);
			message(ERROR, "加载用户订票信息失败!", WC);

			// 释放无效内存
			free_booked_list(booked_info_head);

			return FAIL;
		}

		fread(booked_info_pt, sizeof(booked_info), 1, fp);

		// 检测订票信息与用户的从属关系
		if (strcmp(psngr_id, booked_info_pt->master))
		{
			message(ERROR, "加载的用户订票信息不属于本用户!", NULL);
			message(WARN, "加载用户订票信息完全失败!", WC);

			// 释放无效内存空间
			free_booked_list(booked_info_head);
			free(booked_info_pt);

			return FAIL;
		}

		// 链上用户订票信息
		booked_info_pt = booked_info_head->next;
		booked_info_head = booked_info_head->next;
	}

	if (amount)		// 订票信息数不为0才封尾
	{
		booked_info_pt->next = NULL;
	}
	fclose(fp);

	message(INFO, "订票信息加载成功。", NULL);

	return OK;
}



/////////////////////////////////////////////////////////////////////////////
// 函数功能:	从文件加载用户的系统消息
//
// 函数参数:	sysmsg_head:用户的系统消息链表头结点指针
//				psngr_id:用户名
//				amount:系统消息(包括新旧)总数量
//
/////////////////////////////////////////////////////////////////////////////
status load_sysmsg_list(msgbox_item *sysmsg_head, char *psngr_id, int amount)
{
	FILE *fp;
	char filepath[MAX_FILE_PATH_LEN];
	int i;
	msgbox_item *msgbox_item_pt;

	printf("【信息】正在加载用户(%s)的系统消息...\n", psngr_id);

	// 生成文件路径
	create_ii_dir(filepath, USER_INFO_DIR, psngr_id, MSGBOX_FILE);

	// 检测文件有效性
	if (!check_data(filepath, 1))
	{
		message(ERROR, "从文件加载用户系统消息失败!", WC);

		return FAIL;
	}

	// 只读二进制方式打开文件读取信息
	fp = fopen(filepath, "rb");

	for (i = 0; i < amount; i++)
	{
		if (!(msgbox_item_pt = (msgbox_item *)malloc(sizeof(msgbox_item))))
		{
			message(ERROR, "加载用户系统消息时申请内存失败!", NULL);
			message(ERROR, "加载用户系统消息失败!", WC);

			// 释放无效内存
			free_user_item_info_list(sysmsg_head);

			return FAIL;
		}

		fread(msgbox_item_pt, sizeof(booked_info), 1, fp);

		// 检测用户系统消息与用户的从属关系
		if (strcmp(psngr_id, msgbox_item_pt->receiver))
		{
			message(ERROR, "加载的用户系统消息不属于本用户!", NULL);
			message(WARN, "加载用户系统消息完全失败!", WC);

			// 释放无效内存空间
			free_user_item_info_list(sysmsg_head);
			free(msgbox_item_pt);

			return FAIL;
		}

		// 链上用户订票信息
		msgbox_item_pt = sysmsg_head->next;
		sysmsg_head = sysmsg_head->next;
	}
	
	if (amount)			// 如果消息数不为0才封尾
	{
		msgbox_item_pt->next = NULL;
	}
	fclose(fp);

	message(INFO, "系统消息加载成功。", NULL);

	return OK;
}



/////////////////////////////////////////////////////////////////////////////
#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -