c++ - Reading objects from a binary file -
i wrote program, lets enter information car, , writes object on binary file.
car car_obj; std::ofstream entr; std::ifstream ex; void save_data() { entr.open("main.bin", std::ios::app | std::ios::binary); car_obj.setinfo(); entr.write((char*)&car_obj, sizeof (car_obj)); entr.close();
}
after this, have function reading binary file in order display cars have fuel consumption less user enters (the number factr in function arguments).
void vehicles_under_factr(float factr) { ex.open("main.bin", std::ios::app | std::ios::binary); while (ex.read((char*)&car_obj, sizeof (car_obj)) && !ex.eof()) { if (car_obj.fuel_cons < factr) { car_obj.displayinfo(); } } ex.close();
}
it works fine, if have three, or less, objects in binary file. when there more three, displays rest empty fields. if there no access after third car. why so, , can it?
i feel close solution, knowledge limited. thank in advance!
no, can't this,read((char *)obj, sizeof(obj)),because every object in program have memory address, allocate system. can't read address file .if object contain data, work fine, if it's element object , attribute related memory address or iterator(related memory address). work fail.
Comments
Post a Comment