I am currently working on a game project and i am saving a struct with all needed values of a game-object into a file. Saving is completely fine but if i start loading them in it will only load the first 25 of them.
After that i tried with using a while (true)
that loads in objects until the last object doesn't get the right type and break out of the loop. This works perfectly fine but is maybe not the most elegant solution to take.
using namespace std;
struct{
const char* type;
int sizeW;
int sizeH;
int collisionLayer;
int textureID;
int positionX;
int positionY;
} loadStruct;
ifstream fileObj;
fileObj.open("level.txt");
if (fileObj.is_open()){
while (!fileObj.eof()){
fileObj.read((char*)&loadStruct, sizeof(loadStruct));
// creating my object out of loadStruct -> working fine
}
fileObj.close();
}
I've tried to remove the check for eof() and simply put a try/catch in the while loop breaking out if error is catched but this idea was not the most elegant and didn't work 100% of the time.