Jump to content


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.

Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.


Generic Code For Writing Structures, Help Me Make It More Generic

  • Please log in to reply
No replies to this topic

#1 chopficaro


  • Members
  • 68 posts
  • Local time:02:05 AM

Posted 16 September 2008 - 12:00 PM

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:

#include <iostream>
#include <fstream>
#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:
		//option 1, read from the file:
		if (choice==1)
		//option 2, write over the file:
		if (choice==2)
		//option 3, append the file:
		if (choice==3)
	//option 4, exit:
	}while (choice!=4);
	return 0;
#include <iostream>
#include <fstream>
#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();
		int fileSize=(end-begin)/(sizeof(structi));
		pStructi= new grades [fileSize];
	else cout << "\nUnable to open file for size check\n";
	//read the file into memory:
	int structArraySize=0;
	ifstream filei;
	filei.open (readFileName, ios::in|ios::binary);
	if (filei.is_open())
		while ((filei.peek()!=EOF))
			filei.read(reinterpret_cast<char*>(&pStructi[structArraySize]), sizeof(grades));
		//display memory on screen:
	else cout << "\nUnable to open file for input\n";

void wrtfile(char wrtFileName[],char wrtMessage[],grades structo,grades * pStructo)
	ofstream fileo;
	fileo.open (wrtFileName, ios::out|ios::binary);
	cout << wrtMessage;
	if (fileo.is_open())
		fileo.write (reinterpret_cast<char*>(pStructo), sizeof(structo));
	else cout << "\nUnable to open file for output\n";

void appfile(char appFileName[],char appMessage[],grades structa,grades * pStructa)
	ofstream filea;
	filea.open (appFileName, ios::app|ios::binary);
	cout << appMessage;
	if (filea.is_open())
		filea.write (reinterpret_cast<char*>(pStructa), sizeof(structa));
	else cout << "\nUnable to open file for output\n";

void scanstruct(grades * pStructscan)
	cin >> (*pStructscan).grade1 >> (*pStructscan).grade2 >> (*pStructscan).grade3 >> (*pStructscan).grade4;

void printmemory(int specificStructArraySize,grades * pStructi)
	for(int i=0;i<specificStructArraySize;i++)
		cout<<"\ngrade1\n"<< (pStructi[i]).grade1<<"\ngrade2\n"<< (pStructi[i]).grade2<<"\ngrade3\n"<< (pStructi[i]).grade3<<"\ngrade4\n"<< (pStructi[i]).grade4<<"\n";

struct grades
	int grade1;
	int grade2;
	int grade3;
	int grade4;

void printmemory(int ,grades * );
void scanstruct(grades * );
void readfile(char [],grades ,grades * );
void wrtfile(char [],char [],grades ,grades * );
void appfile(char [],char [],grades ,grades * );


BC AdBot (Login to Remove)


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users