saving mutliple variables.......
I am trying to make a program that should save two variables in to a file.....
Code:
#include <fstream.h>
#include <stdio.h>
#include <windows.h>
struct fCOORD
{
float x,y;
};
struct Table
{
unsigned int TEXTURE;
float xpos,ypos,zpos;
float rot;
fCOORD point[4];
float slope[4];
float b[4];
};
struct Door
{
unsigned int dTEXTURE;
unsigned int kTEXTURE;
float xpos,ypos,zpos;
float rot;
fCOORD point[4];
float slope[4];
float b[4];
bool open;
};
struct Chair
{
unsigned int TEXTURE;
float xpos,ypos,zpos;
float rot;
fCOORD point[4];
float slope[4];
float b[4];
};
struct B3DSaveHeader
{
float level[30] [30];
int numTables;
int numDoors;
int numChairs;
};
struct B3DSaveObjects
{
Door *door;
Table *table;
Chair *chair;
};
FILE *savefile;
void main()
{
B3DSaveHeader level1;
B3DSaveObjects level1O;
for(int c=0;c<30;c++)
{
for(int d=0;d<30;d++)
{
level1.level[c] [d]=(float)(rand()%100)/100.0f;
}
}
level1.numChairs=1;
level1.numDoors=0;
level1.numTables=0;
level1O.chair = new Chair[level1.numChairs];
level1O.door = new Door[level1.numDoors];
level1O.table = new Table[level1.numTables];
level1O.chair[0].rot=15;
savefile = fopen("DEFAULT.B3D","wb");
if (savefile)
{
fwrite(reinterpret_cast<const void*>(&level1),sizeof(level1),1,savefile);
fwrite(reinterpret_cast<const void*>(&level1O),sizeof(level1O),1,savefile);
fclose(savefile);
}
else
{}
B3DSaveHeader level;
B3DSaveObjects levelO;
savefile=fopen("DEFAULT.B3D","rb");
if(savefile)
{
fread(reinterpret_cast<void*>(&level),sizeof(level),1,savefile);
levelO.chair = new Chair[level.numChairs];
levelO.door = new Door[level.numDoors];
levelO.table = new Table[level.numTables];
fread(reinterpret_cast<void*>(&levelO),sizeof(levelO),1,savefile);
fclose(savefile);
}
cout << level.numChairs << endl;
cout << level.numDoors << endl;
cout << level.numTables << endl;
cout << levelO.chair[0].rot << endl;
}
and that works fine, it saves it, closes it, then opens it again, but when I try and comment out the loading part, and just saving it and compiling it, then I comment out the saving part and uncomment the loading part, and compile it, it puts out some weird number....
IE, The program will only load the correct files if you don't exit it, but when you exit it and load again, the file is messed up.....
can anyone help me with this?
ok then, it's in the same post, lol
ok, evidently nobody knows how to fix my last problem.....so here's a simple question:
are you allowed to write multiple variables with fwrite? and then read the variables with fread?
like so:
Code:
fwrite(&level1,sizeof(level1),1,savefile);
fwrite(&level1O,sizeof(level1O),1,savefile);
fclose(savefile);
and then in another program, that will read the code you just saved:
Code:
fread(&level,sizeof(B3DSaveHeader),1,savefile2);
levelO.chair = new Chair[level.numChairs];
levelO.door = new Door[level.numDoors];
levelO.table = new Table[level.numTables];
fread(&levelO,sizeof(levelO),1,savefile2);
fclose(savefile2);
whenever i try to do this, it crashes!
-EDIT-
oh, and the reason i'm using new is because the three levelO things are dynamic arrays of structures, look at my other post to see the full code