Hỏi đáp

Chia sẻ kiến thức, cùng nhau phát triển

Ghi và đọc một struct có DSLK đơn xuống file nhị phân

00:12 20-04-2017 2.189 lượt xem 1 bình luận 07:09 20-04-2017

Mình có 2 struct như thế này. khi mình lưu thông tin của 1 máy bay thì thì khi đọc lên thì không đọc được cái DSLKĐ chuyến bay. Vì bản chất là ghi địa chỉ con trỏ. Mọi người giúp mình làm sao để đọc ghi file với ạ.Bên dưới là cấu trúc và hàm đọc ghi file của mình

//may bay
struct Airplane
{
	char id[30];
	int nChair;
	LIST_FLIGHT listFlight;
	
};
typedef struct Airplane AIRPLANE;
typedef AIRPLANE* PTR_AIRPLANE;
struct ListAirplane
{
	int n = 0;
	PTR_AIRPLANE *listAirplane = new PTR_AIRPLANE[MAX_AIRPLANE];
};
typedef struct ListAirplane LIST_AIRPLANE;
typedef LIST_AIRPLANE* PTR_LIST_AIRPLANE;



//chuyen bay
struct Flight
{
	int nTicket = 0;
	char id[30], destiny[100];
	DateTime dateLeave;
	int stt = STT_REMAIN_TICKET;
	int  nTicketSold = 0, nTicketRemain = 0;
	Ticket *listTicket = new Ticket[MAX_TICKET];
};
typedef struct Flight FLIGHT;


struct NodeFlight
{
	FLIGHT data;
	struct NodeFlight *pNext;
};
typedef struct NodeFlight NODE_FLIGHT;
struct ListFlight
{
	NODE_FLIGHT *pHead, *pTail;
};
typedef struct ListFlight LIST_FLIGHT;




//ghi và đọc file
void SaveAirplane(PTR_LIST_AIRPLANE pListAirplane) {
	fstream myfile("airplane.txt", ios::out | ios::binary);
	int SizeData = sizeof(AIRPLANE);
	for (int i = 0; i < pListAirplane->n; i++) {
		myfile.write(reinterpret_cast<const char*>(pListAirplane->listAirplane[i]), SizeData);
	}
	myfile.close();



}
void LoadAirplane(PTR_LIST_AIRPLANE &pListAirplane) {
	fstream Myfile;
	Myfile.open("airplane.txt", ios::in | ios::binary);
	PTR_AIRPLANE temp;
	while (!Myfile.eof())
	{
		temp = new AIRPLANE;
		Myfile.read(reinterpret_cast<char*>(temp), sizeof(AIRPLANE));
		if (!Myfile.eof()) pListAirplane->listAirplane[pListAirplane->n++] = temp;
		else delete temp;
	}
	Myfile.close();
}

 

Bình luận

Để bình luận, bạn cần đăng nhập bằng tài khoản Howkteam.

Đăng nhập
K9 SuperAdmin, KquizAdmin, KquizAuthor đã bình luận 07:09 20-04-2017

Mình nghĩ là nếu lưu xuống là thông tin. Thì khi đưa lên. Bạn sẽ đọc dữ liệu sau đó rồi tạo ra một con trỏ để đưa giá trị vào

Câu hỏi mới nhất