?? train_booking_fread.c
字號:
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 + -