fwrite and fread help

This is a discussion on fwrite and fread help within the C++ Programming forums, part of the General Programming Boards category; Code: #include <fstream.h> #include <stdio.h> #include <windows.h> struct B3DSave { float level[30] [30]; int numTables; int numDoors; int numChairs; }; ...

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

    fwrite and fread help

    Code:
          
    #include <fstream.h>
    #include <stdio.h>
    #include <windows.h>
    
    struct B3DSave
    {
    	float level[30] [30];
    	int numTables;
    	int numDoors;
    	int numChairs;
    };
    
    void main()
    {
    	B3DSave level1;
    
    	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=3;
    	level1.numDoors=5;
    	level1.numTables=8;
    
    	FILE *savefile;
    	savefile = fopen("DEFAULT.B3D","wb");
    	//Open for write binary!
    	if (savefile) //Check to see if it opened
    	{
    		fwrite(level1,sizeof(B3DSave),1,savefile);
    		fclose(savefile);
    	}
    	else
    	{}
    
    	B3DSave level;
    
    	savefile=fopen("DEFAULT.B3D","rb");
    
    	if(savefile)
    	{
    		fread(level,sizeof(B3DSave),1,savefile);
    		fclose(savefile);
    	}
    	cout << level.numChairs << endl;
    	cout << level.numDoors << endl;
    	cout << level.numTables << endl;
    }


    what I'm trying to do is write a structure to the file, but it says that the variable has to be::

    :\BloodRight3D\mousetest.cpp(34) : error C2664: 'fwrite' : cannot convert parameter 1 from 'struct B3DSave' to 'const void *'
    No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

    and then it doesn't work....theoretically this should work though, any ideas? (i haven't used fread/fwrite that much....)

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Code:
    fwrite(level1,sizeof(B3DSave),1,savefile);
    Change that to:

    Code:
    fwrite(reinterpret_cast<const void*>(&level1), sizeof(B3DSave), 1, save);

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2d array and fwrite and fread
    By totalnewbie in forum C Programming
    Replies: 14
    Last Post: 01-10-2009, 03:45 PM
  2. Replies: 2
    Last Post: 12-26-2008, 04:38 PM
  3. Weird problem with fwrite() and fread()
    By piote in forum C Programming
    Replies: 2
    Last Post: 11-13-2004, 03:07 PM
  4. buffer type for fread & fwrite
    By daluu in forum C Programming
    Replies: 5
    Last Post: 05-08-2003, 07:57 PM
  5. fread() and fwrite() ?
    By Limblet in forum C Programming
    Replies: 4
    Last Post: 09-25-2001, 08:36 PM

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