Đọc/ghi file nhị phân bị lỗi khi dùng cây nhị phân tìm kiếm

mình đang viết hai hàm ghi và đọc file nhị phân, hàm ghi thì mình test chạy được, vào file mở lên thì có dữ liệu, nhưng hàm đọc thì bị lỗi, lúc đọc ra thì nó không đọc được, ai có thể xem qua giúp mình được không ạ? fix mấy ngày nay vẫn chưa tìm ra được lỗi tại sao nó lại không chạy được TT :frowning: (mình dùng dev c)

#include <iostream>
#include <conio.h>
#include <string>
#include <string.h>
#include <fstream>
#include <time.h>
#include <stdlib.h>

//so lieu may bay
#define MAXLIST 300
#define MAXTICKET 300

// trang thai chuyen bay
#define TT_HUYCHUYEN 0
#define TT_CONVE 1
#define TT_HETVE 2
#define TT_HOANTAT 3

// tinh trang ve
#define TICHKET_DABAN 1
#define TICHKET_TRONG 0

using namespace std;

string arrIdHanhKhach[300];

int NgayTrongNam[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

struct DateTime{
	int ngay = 0;
	int  thang = 0;
	int  nam = 0;
	int gio = 0;
	int phut = 0;
};

bool KiemTraDateTime(DateTime dt){
	NgayTrongNam[2] = 28;
	if ((dt.nam % 4 == 0 && dt.nam % 100 != 0) || (dt.nam % 400 == 0)) // nam nhuan la nam chia het cho 400 hoac chia het cho 4 va khong chia het cho 100
		NgayTrongNam[2] = 29;
	if (dt.ngay > NgayTrongNam[dt.thang]) return false;
	return true;
}
//======================MAY BAY=============================
struct PData { 
	char SoHieu[15];
	char LoaiMayBay[40];
	int SoCho;
};

struct AirPlanes {
	PData *data[MAXLIST];
	int n;
};

//=======================CHUYEN BAY==========================
struct Ticket {
	int tinhTrang = TICHKET_TRONG; 
	int SttTicket = NULL;
};

struct Flight {
	char id[15];
	DateTime Ngaykhoihanh;
	char SanBayden[100];
	int TrangThai = TT_CONVE;
	int SoVeDaBan = 0;
	char SoHieu[15];
	Ticket *DanhSachVe;
};

struct nodeFlight {
 	Flight DataFlight;
 	struct nodeFlight *Next;
};
typedef struct nodeFlight *NODE_CHUYENBAY;

NODE_CHUYENBAY taoNodeFlight(Flight data){ // ham tao moi 1 node
	NODE_CHUYENBAY p = new nodeFlight;
	if (p == NULL) return NULL;
	p -> DataFlight = data; // gan gia tri cho node
	p -> Next = NULL; // cho next tro toi NULL
	return p;
}
//========================HANH KHACH==========================
struct HanhKhach{
	string cmnd;
	char Ho[20], Ten[20];
	int phai = 1; // 1 la nam, 2 la nu
	//bool daDatVe = false;	
};

struct nodeHanhKhach{
	HanhKhach data;
	struct nodeHanhKhach *left;
	struct nodeHanhKhach *right;
};
typedef struct nodeHanhKhach NODE_HANHKHACH;
typedef NODE_HANHKHACH* treeHanhKhach; // su dung treeHanhKhach cho de nhin, thay vi phai NODE_HANHKHACH*

void Initialize(treeHanhKhach &root){ // khoi tao cay hanh khach
	//SoHanhKhach = -1;
	root = NULL;
}

void Insert_HanhKhach(treeHanhKhach &t, HanhKhach hk, int &SoTT){
	if (t == NULL){ // cay rong
		NODE_HANHKHACH *point = new NODE_HANHKHACH; // khoi tao 1 node de them vao cay
		point->data = hk;
		point->left = NULL;
		point->right = NULL;
		t = point; // node point chinh la nut goc
		arrIdHanhKhach[++SoTT] = hk.cmnd;
	}
	else { // cay co ton tai phan tu
		if (hk.cmnd.compare(t->data.cmnd) < 0)
			Insert_HanhKhach(t->left, hk, SoTT);
		else if (hk.cmnd.compare(t->data.cmnd) > 0)
			Insert_HanhKhach(t->right, hk, SoTT);
	}
}

void Create_Tree(treeHanhKhach &root, int &SoHanhKhach){
	HanhKhach hk;
	SoHanhKhach = 0;
	int temp;
	cout << "\nNhap so Hanh khach : ";
	cin >> temp;
	for (int i = 1; i <= temp; i++){
		cout << "\nNhap cmnd cua Hanh Khach : ";
		cin >> hk.cmnd;
		while (hk.cmnd.length() != 9 && hk.cmnd.length() != 12){
			cout << "\nMa cmnd khong hop le. Xin vui long nhap lai !";
			cout << "\nNhap cmnd cua Hanh Khach : ";
			cin >> hk.cmnd;	
		}
		cout << "\nNhap Ho :";
		cin >> hk.Ho;
		cout << "\nNhap Ten : ";
		cin >> hk.Ten;
		cout << "\nPhai la : ";
		cin >> hk.phai;
		while (hk.phai != 2 && hk.phai != 1){
			cout << "\nKhong hop le. Moi nhap lai !";
			cin >> hk.phai;
		}
		Insert_HanhKhach(root, hk, SoHanhKhach);
	}
}

//=========================DOC/GHI FILE========================

void GhiHanhKhachVaoFile(treeHanhKhach p, fstream &fileOut, int size) {
	if (p != NULL) {
		fileOut.write(reinterpret_cast<const char*>(&p->data), size);
		GhiHanhKhachVaoFile(p->left, fileOut, size);
		GhiHanhKhachVaoFile(p->right, fileOut, size);
	}
}

void SaveHanhKhach(treeHanhKhach point, int SoHanhKhach) {
	fstream fileOut("E:\\Bài\\Code\\do an\\hanhkhach.txt", ios::out | ios::binary);
	fileOut.write(reinterpret_cast<const char*>(&SoHanhKhach), sizeof(int));
	int size = sizeof(HanhKhach);
	treeHanhKhach p = point;
	GhiHanhKhachVaoFile(p, fileOut, size);
	fileOut.close();
}

void LoadHanhKhach(treeHanhKhach &t, int &SoHanhKhach) {
	fstream fileIn("E:\\Bài\\Code\\do an\\hanhkhach.txt", ios::in | ios::binary);
	int SoTT = -1;
	HanhKhach hk;
	fileIn.read(reinterpret_cast<char*>(&SoHanhKhach), sizeof(int));
	int size = sizeof(HanhKhach);
	for (int i = 0; i < SoHanhKhach; i++)
	{
		fileIn.read(reinterpret_cast<char*>(&hk), size);
		Insert_HanhKhach(t, hk, SoTT);
	}
}

void xuat(HanhKhach p){
	cout << "\n===============";
	cout << "\ncmnd " << p.cmnd;
	cout << "\nho " << p.Ho;
	cout << "\nten " << p.Ten;
	cout << "\nphai " << p.phai;
}
void output(treeHanhKhach t){
	if (t != NULL){
		output(t->left);
		xuat(t->data);
		output(t->right);
	}
}

int main() {
	int SoHanhKhach;
	treeHanhKhach r;
	Initialize(r);
	//Create_Tree(r, SoHanhKhach);
	//GhiSoLuong(SoHanhKhach, "SoLuong.txt");
	//DocSoLuong(SoHanhKhach, "SoLuong.txt");
	LoadHanhKhach(r, SoHanhKhach);
	//SaveHanhKhach(r, SoHanhKhach);	
	
	output(r);
	return 0;
}

mọi người xem phần hành khách với đọc ghi file thôi ạ

83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?