不知道错哪儿了,Vector的操作不会
//片段:有个Hotel类,
class Hotel {
public:
int hotel_code;
char hotel_name[50];
char hotel_city[50];
int total_regular_rooms;
double tariff_regular_room;
int available_regular_rooms;
int total_deluxe_rooms;
double tariff_deluxe_room;
int available_deluxe_rooms;
}
//.....
//省略
//.....
vector<Hotel> Vh(1);
Hotel h;
ofstream OutputFile;
ifstream InputFile;
//....前面从cin读进了几条Hotel的记录然后准备望文件里写.(不知道Vh.size()这个有没问题,
我明明只输入了一条记录,但Vh.size()返回的却是2,总是成倍的???????
)
for(int n=0;n<Vh.size();n++){
OutputFile.write((char*)&Vh[n],sizeof(Vh[n]));
}
InputFile.read((char*)&h,sizeof(h));
while(!InputFile.eof())
{
Vh.push_back(h);
InputFile.read((char*)&h,sizeof(h));
}
InputFile.close();
InputFile.clear();
//下面我输出从文件里读出来的记录,下面的内容是对的,但是老是会在前面先打出一大堆乱码来,
//why??
for(int n=0;n<Vh.size();n++){
cout<<Vh[n].hotel_code<<endl;
cout<<Vh[n].hotel_name<<endl;
cout<<Vh[n].hotel_city<<endl;
}
谢谢各位老大.