
-----------------------------------
chopficaro
Tue Sep 16, 2008 12:01 pm

generic code for writing structures, help me make it more generic
-----------------------------------
well ive got a program that scans a structure from cin and puts it in a binary file, and can also take it out of the binary file and display it. i like the interface ive made very much, i want to use this code for many many projects without having to change much,

especially for many different types of structures,

so im trying to make it more generic, but i havent a clue how to proceed. my guess is that ill have to use some element of classes.

im not asking u to write my code i just want a hint. anything at all would be greatly appreciated

take a look:

exportimportstructmain.cpp
#include 
#include 
#include "exportimportstructdec.h"
using namespace std;

grades myGradesi,myGradeso,myGradesa;

//declare pointers to structures for file io:
grades * pMyGradeso=&myGradeso;
grades * pMyGradesa=&myGradesa;
grades * pMyGradesi;

int main ()
{
	int choice;
	char opFile[]="binary.bin";
	char opMessage[]="\nplease enter 4 grades separated by pressing enter\n";
	char menu[]="\ndo u want to \n1, read from the file, or \n2, write over the file or \n3, append the file or \n4, exit the program?\n";
	//give the user options:
	do
	{
		coutchoice;
		//option 1, read from the file:
		if (choice==1)
		{
			readfile(opFile,myGradesi,pMyGradesi);
		}
		//option 2, write over the file:
		if (choice==2)
		{
			wrtfile(opFile,opMessage,myGradeso,pMyGradeso);
		}
		//option 3, append the file:
		if (choice==3)
		{
			appfile(opFile,opMessage,myGradesa,pMyGradesa);
		}
	//option 4, exit:
	}while (choice!=4);
	return 0;
}
exportimportstructdef.cpp
#include 
#include 
#include "exportimportstructdec.h"
using namespace std;

extern grades myGradesi, myGradeso, myGradesa;
extern grades *pMyGradeso, *pMyGradesa, *pMyGradesi;

//the purpose of this program is to send structures into a file, and then read them from the same file
//structure to be written:


void readfile(char readFileName[],grades structi,grades * pStructi)
{
	//check the size of the file and allocate appropriate memory:
	long begin,end;
	ifstream filecheck (readFileName);
	if (filecheck.is_open())
	{
		begin = filecheck.tellg();
		filecheck.seekg (0, ios::end);
		end = filecheck.tellg();
		filecheck.close();
		int fileSize=(end-begin)/(sizeof(structi));
		pStructi= new grades [fileSize];
	}
	else cout  (*pStructscan).grade3 >> (*pStructscan).grade4;
}

void printmemory(int specificStructArraySize,grades * pStructi)
{
	for(int i=0;i