saving mutliple variables.......

This is a discussion on saving mutliple variables....... within the C++ Programming forums, part of the General Programming Boards category; I am trying to make a program that should save two variables in to a file..... Code: #include <fstream.h> #include ...

  1. #1
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,904

    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?

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,904

    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

  3. #3
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    Thanks, much cleaner this way!

  4. #4
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,904
    and another question.........

    I'm trying to write a program that will create dynamic arrays, and save a file. First it saves a header with some variables saying the amount of objects I want in the dynamic arrays, then it saves the Objects to the file, so it's like this:

    block 1 of FILE: HEADER
    level[30] [30]
    int numTables=0
    int numDoors=0
    int numChairs=2

    block 2 of FILE: OBJECTS
    Door *door[0]
    Table *table[0]
    Chair *chair[2]

    that's just some pseudo explanation, hopefully you understand, now, what I want to do is have another program that will open up this file, and read the first block in to another header, look at how many of each object you need, then allocate the amount of space you need in the objects structure so that it will fit all of the info, then load the OBJECTS block......get it?

  5. #5
    xmdvp
    Guest
    I hope you understand that your structure B3DSaveObjects contains pointers, and therefore its sizeof() return 12 (in 1 bytes alignement structures): 3 * sizeof(pointer)

    so you're basically saving three pointers to a file, then don't expect values to be good when you reload them, because they don't point at anything that should be valid if you're lucky (you would have been very unlucky if it did work!)

    the reason why it works when you don't comment anything out is because the pointer values are still valid when you reload the data!!!

    fread() and fwrite() as you wrote them are not making any sense for a dynamic allocation.

    xmdvp

  6. #6
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,904
    sooo, how would I write them to the file then? without having to use dynamic arrays......

    -EDIT-
    or would the only way to do it would be to use static arrays?

    ex:

    Doors door[50];

    etc....?
    Last edited by jverkoey; 02-13-2003 at 06:35 PM.

  7. #7
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,904
    AH HAH!! nevermind, I got it so I can still have dynamic arrays, but now it creates the dynamic array, and then does a for loop to save the objects individually. that way they are being saved as actual OBJECTS and not pointers

    thanks for the help

  8. #8
    xmdvp
    Guest
    if you want to save a whole structure all at once in a file then it's static structures. You have no choice.

    if you want dynamic you have to do things a different way.

    You are in C++ right ? It's OOP meaning objects. instead of having structures, you should have classes, these classes should have methods like : save(FILE *fp) and load (FILE *fp) because they know how to be saved and how to be loaded.

    like :

    Code:
    class X {
    public:
    	X(){
    		array = new char[1];	// 
    		array[0] = 0;
    		array_size = 1;
    	}
    
    	~X(){}
    
    	bool save(FILE *fp) {
    		if( array == NULL ) return false;
    		// first, save the array_size
    		fwrite(&array_size, sizeof(array_size), 1, fp);
    		// then the whole contents of the array
    		fwrite(array, sizeof(*array), array_size, fp);
    
    		return true;
    	}
    
    	void load(FILE *fp) {
    		// load the array size
    		fread(&array_size, sizeof(array_size), 1, fp);
    
    		// load the array contents depending on the size read
    		delete [] array;	// deleting NUll pointers is not a problem in C++
    		array = new char [array_size];
    		fread(array, sizeof(*array), array_size, fp);
    	}
    
    public:
    	// your public members - as if it was a structure.
    	// although this isn't appropriate for a class. anyway
    	char *array;
    	int array_size;
    };

    The only thing you have to be carefull about is to call the loads and the save in the right order.

    this is a simple case, plus that's what we call C++.

  9. #9
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,904
    hey, i'm not stupid, ok? the code i placed above was just a testing program to even see if it worked. My actual game code is a HUGE class (1110 lines of code, all hand-typed) that has everything the game needs to run, and all you have to do is create one, then run

    name.runEverything

    and that'll run EVERY LINE OF CODE. I know what OOP is, and I know what C is. I am not stupid.

  10. #10
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    >>(1110 lines of code, all hand-typed)

    As opposed to toe-typed, i take it?

  11. #11
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,904
    lmao (as opposed to copying)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 01:54 AM
  2. Replies: 15
    Last Post: 09-30-2008, 03:12 AM
  3. esbo's data sharing example
    By esbo in forum C Programming
    Replies: 49
    Last Post: 01-08-2008, 11:07 PM
  4. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 04:25 AM
  5. Declaring an variable number of variables
    By Decrypt in forum C++ Programming
    Replies: 8
    Last Post: 02-27-2005, 04:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21