my first .h file

im making my frist multiple file compilation, and my first .h file of my own.
problem is, i dont know where to put struct grades {definition}. no matter which file i put it in, the compiler complains




exportimportstructmain.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <fstream>
#include "exportimportstructdec.h"
using namespace std;

struct grades
{
	int grade1;
	int grade2;
	int grade3;
	int grade4;
}mygradesi,mygradeso,mygradesa;

//declare pointers to structures for file io:
grades * mygradesptro=&mygradeso;
grades * mygradesptra=&mygradesa;
grades * mygradesptri;


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
	{
		cout<<menu;
		cin>>choice;
//option 1, read from the file:
		if (choice==1)
		{
			readfilefunc(opfile,mygradesi,mygradesptri);
		}
//option 2, write over the file:
		if (choice==2)
		{
			wrtfilefunc(opfile,opmessage,mygradeso,mygradesptro);
		}
//option 3, append the file:
		if (choice==3)
		{
			appfilefunc(opfile,opmessage,mygradesa,mygradesptra);
		}
//option 4, exit:
	}while (choice!=4);
	return 0;
}

exportimportstructdef.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>
#include <fstream>
#include "exportimportstructdec.h"
using namespace std;

struct grades
{
	char grade1[10];
	int grade2;
	int grade3;
	int grade4;
}mygradesi,mygradeso,mygradesa;

//declare pointers to structures for file io:
grades * mygradesptro=&mygradeso;
grades * mygradesptra=&mygradesa;
grades * mygradesptri;


//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 readfilefunc(char filename1[],grades structi,grades * structptri)
{
//check the size of the file and allocate appropriate memory:
	long begin,end;
	ifstream filecheck (filename1);
	if (filecheck.is_open())
	{
		begin = filecheck.tellg();
		filecheck.seekg (0, ios::end);
		end = filecheck.tellg();
		filecheck.close();
		int filesize=(end-begin)/(sizeof(structi));
		structptri= new grades [filesize];
	}
	else cout << "Unable to open file for size check";
//read the file into memory:
	int structarraysize=0;
	ifstream filei;
	filei.open (filename1, ios::in|ios::binary);
	if (filei.is_open())
	{
		while ((filei.peek()!=EOF))
		{
			filei.read(reinterpret_cast<char*>(&structptri[structarraysize]), sizeof(grades));
			structarraysize++;
		}
//display memory on screen:
		printmemory(structarraysize,structptri);
	}
	else cout << "Unable to open file for input";
}


void wrtfilefunc(char wrtfilename[],char wrtmessage[],grades structo,grades * structptro)
{
	ofstream fileo;
	fileo.open (wrtfilename, ios::out|ios::binary);
	cout << wrtmessage;
	scanstruct(structptro);
	if (fileo.is_open())
	{
		fileo.write (reinterpret_cast<char*>(structptro), sizeof(structo));
		fileo.close();
	}
	else cout << "\nUnable to open file for output\n";
}

void appfilefunc(char appfilename[],char appmessage[],grades structa,grades * structptra)
{
	ofstream filea;
	filea.open (appfilename, ios::app|ios::binary);
	cout << appmessage;
	scanstruct(structptra);
	if (filea.is_open())
	{
		filea.write (reinterpret_cast<char*>(structptra), sizeof(structa));
		filea.close();
	}
	else cout << "\nUnable to open file for output\n";
}

void scanstruct(grades * structptrscan)
{
	cin >> (*structptrscan).grade1 >> (*structptrscan).grade2 >> (*structptrscan).grade3 >> (*structptrscan).grade4;
}

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

exportimportstructdec.h
1
2
3
4
5
6
struct grades;
void printmemory(int specificstructarraysize,grades * structptri);
void scanstruct(grades * structptrscan);
void readfilefunc(char filename1[],grades structi,grades * structptri);
void wrtfilefunc(char wrtfilename[],char wrtmessage[],grades structo,grades * structptro);
void appfilefunc(char wrtfilename[],char wrtmessage[],grades structo,grades * structptro);


1>------ Build started: Project: exportstruct, Configuration: Debug Win32 ------
1>Linking...
1>exportimportstructmain.obj : error LNK2005: "struct grades * mygradesptri" (?mygradesptri@@3PAUgrades@@A) already defined in exportimportstructdef.obj
1>exportimportstructmain.obj : error LNK2005: "struct grades mygradeso" (?mygradeso@@3Ugrades@@A) already defined in exportimportstructdef.obj
1>exportimportstructmain.obj : error LNK2005: "struct grades mygradesa" (?mygradesa@@3Ugrades@@A) already defined in exportimportstructdef.obj
1>exportimportstructmain.obj : error LNK2005: "struct grades mygradesi" (?mygradesi@@3Ugrades@@A) already defined in exportimportstructdef.obj
1>exportimportstructmain.obj : error LNK2005: "struct grades * mygradesptro" (?mygradesptro@@3PAUgrades@@A) already defined in exportimportstructdef.obj
1>exportimportstructmain.obj : error LNK2005: "struct grades * mygradesptra" (?mygradesptra@@3PAUgrades@@A) already defined in exportimportstructdef.obj
1>C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\exportstruct\Debug\exportstruct.exe : fatal error LNK1169: one or more multiply defined symbols found
1>Build log was saved at "file://c:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\exportstruct\exportstruct\Debug\BuildLog.htm"
1>exportstruct - 7 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Last edited on
You are defining the structure/the pointer/ref variables multiple times.

Define the variables and the structure inside the .h (i.e. move
1
2
3
grades * mygradesptro=&mygradeso;
grades * mygradesptra=&mygradesa;
grades * mygradesptri;
and
1
2
3
4
5
6
7
struct grades
{
	char grade1[10];
	int grade2;
	int grade3;
	int grade4;
}mygradesi,mygradeso,mygradesa;
into exportimportstructdec.h

Then remove that code from the other two files.

EDIT: Oh, and on your .h file...at the top add this:

1
2
#ifndef EXPORTIMPORTSTRUCTDEC_H
#def EXPORTIMPORTSTRUCTDEC_H 

and at the bottom of the file add:
#endif

That will prevent errors if you include the .h in multiple .cpps (which you are)
Last edited on
ive tried that before, now i have obj errors

1>------ Build started: Project: exportstruct, Configuration: Debug Win32 ------
1>Compiling...
1>exportimportstructdef.cpp
1>exportimportstructmain.cpp
1>Generating Code...
1>Linking...
1>exportimportstructmain.obj : error LNK2005: "struct grades * mygradesptri" (?mygradesptri@@3PAUgrades@@A) already defined in exportimportstructdef.obj
1>exportimportstructmain.obj : error LNK2005: "struct grades mygradeso" (?mygradeso@@3Ugrades@@A) already defined in exportimportstructdef.obj
1>exportimportstructmain.obj : error LNK2005: "struct grades mygradesa" (?mygradesa@@3Ugrades@@A) already defined in exportimportstructdef.obj
1>exportimportstructmain.obj : error LNK2005: "struct grades mygradesi" (?mygradesi@@3Ugrades@@A) already defined in exportimportstructdef.obj
1>exportimportstructmain.obj : error LNK2005: "struct grades * mygradesptro" (?mygradesptro@@3PAUgrades@@A) already defined in exportimportstructdef.obj
1>exportimportstructmain.obj : error LNK2005: "struct grades * mygradesptra" (?mygradesptra@@3PAUgrades@@A) already defined in exportimportstructdef.obj
1>C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\exportstruct\Debug\exportstruct.exe : fatal error LNK1169: one or more multiply defined symbols found
1>Build log was saved at "file://c:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\exportstruct\exportstruct\Debug\BuildLog.htm"
1>exportstruct - 7 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========



exportimportstructmain.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <fstream>
#include "exportimportstructdec.h"
using namespace std;


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
	{
		cout<<menu;
		cin>>choice;
//option 1, read from the file:
		if (choice==1)
		{
			readfilefunc(opfile,mygradesi,mygradesptri);
		}
//option 2, write over the file:
		if (choice==2)
		{
			wrtfilefunc(opfile,opmessage,mygradeso,mygradesptro);
		}
//option 3, append the file:
		if (choice==3)
		{
			appfilefunc(opfile,opmessage,mygradesa,mygradesptra);
		}
//option 4, exit:
	}while (choice!=4);
	return 0;
}

exportimportstructdef.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86

#include <iostream>
#include <fstream>
#include "exportimportstructdec.h"
using namespace std;



//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 readfilefunc(char filename1[],grades structi,grades * structptri)
{
//check the size of the file and allocate appropriate memory:
	long begin,end;
	ifstream filecheck (filename1);
	if (filecheck.is_open())
	{
		begin = filecheck.tellg();
		filecheck.seekg (0, ios::end);
		end = filecheck.tellg();
		filecheck.close();
		int filesize=(end-begin)/(sizeof(structi));
		structptri= new grades [filesize];
	}
	else cout << "Unable to open file for size check";
//read the file into memory:
	int structarraysize=0;
	ifstream filei;
	filei.open (filename1, ios::in|ios::binary);
	if (filei.is_open())
	{
		while ((filei.peek()!=EOF))
		{
			filei.read(reinterpret_cast<char*>(&structptri[structarraysize]), sizeof(grades));
			structarraysize++;
		}
//display memory on screen:
		printmemory(structarraysize,structptri);
	}
	else cout << "Unable to open file for input";
}


void wrtfilefunc(char wrtfilename[],char wrtmessage[],grades structo,grades * structptro)
{
	ofstream fileo;
	fileo.open (wrtfilename, ios::out|ios::binary);
	cout << wrtmessage;
	scanstruct(structptro);
	if (fileo.is_open())
	{
		fileo.write (reinterpret_cast<char*>(structptro), sizeof(structo));
		fileo.close();
	}
	else cout << "\nUnable to open file for output\n";
}

void appfilefunc(char appfilename[],char appmessage[],grades structa,grades * structptra)
{
	ofstream filea;
	filea.open (appfilename, ios::app|ios::binary);
	cout << appmessage;
	scanstruct(structptra);
	if (filea.is_open())
	{
		filea.write (reinterpret_cast<char*>(structptra), sizeof(structa));
		filea.close();
	}
	else cout << "\nUnable to open file for output\n";
}

void scanstruct(grades * structptrscan)
{
	cin >> (*structptrscan).grade1 >> (*structptrscan).grade2 >> (*structptrscan).grade3 >> (*structptrscan).grade4;
}

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

exportimportstructdec.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#ifndef EXPORTIMPORTSTRUCTDEC_H
#define EXPORTIMPORTSTRUCTDEC_H 



struct grades;

struct grades
{
	int grade1;
	int grade2;
	int grade3;
	int grade4;
}mygradesi,mygradeso,mygradesa;

//declare pointers to structures for file io:
grades * mygradesptro=&mygradeso;
grades * mygradesptra=&mygradesa;
grades * mygradesptri;



void printmemory(int specificstructarraysize,grades * structptri);
void scanstruct(grades * structptrscan);
void readfilefunc(char filename1[],grades structi,grades * structptri);
void wrtfilefunc(char wrtfilename[],char wrtmessage[],grades structo,grades * structptro);
void appfilefunc(char wrtfilename[],char wrtmessage[],grades structo,grades * structptro);


#endif 
Why do you have it defined like that in the first place, or am I missing something here?

1
2
3
4
5
6
7
struct grades
{
	int grade1;
	int grade2;
	int grade3;
	int grade4;
}MyGrades, *pMyGrades;


Should be all you need right there. MyGrades being the defined name of the structue and pMyGrades being the pointer to the structure. But hey, maybe I'm missing something but it just doesn't make sense to me. I didn't read the whole code by the way, just that one part you said you were having errors with.
its just my syntax, i for input o for output, and a for append. i dont like the same variable names doing different things, its confusing to me
ok, ive taken some suggestions from some people in order to make my code more readable, and ive tried every possible combination of the use of the extern keyword, using it in one cpp file and defining it in anoter, or in the .h... etc, nothing works, im still where i was at my first post. any ideas?

ps: i couldnt fit my whole error report in because of posting rules so i deleted the last few

1>------ Build started: Project: exportstruct, Configuration: Debug Win32 ------
1>Compiling...
1>exportimportstructdef.cpp
1>c:\documents and settings\administrator\my documents\visual studio 2008\projects\exportstruct\exportstruct\exportimportstructdef.cpp(27) : error C2027: use of undefined type 'grades'
1> c:\documents and settings\administrator\my documents\visual studio 2008\projects\exportstruct\exportstruct\exportimportstructdec.h(4) : see declaration of 'grades'
1>c:\documents and settings\administrator\my documents\visual studio 2008\projects\exportstruct\exportstruct\exportimportstructdef.cpp(28) : error C2512: 'grades' : no appropriate default constructor available
1>c:\documents and settings\administrator\my documents\visual studio 2008\projects\exportstruct\exportstruct\exportimportstructdef.cpp(39) : error C2036: 'grades *' : unknown size
1>c:\documents and settings\administrator\my documents\visual studio 2008\projects\exportstruct\exportstruct\exportimportstructdef.cpp(39) : error C2027: use of undefined type 'grades'
1> c:\documents and settings\administrator\my documents\visual studio 2008\projects\exportstruct\exportstruct\exportimportstructdec.h(4) : see




exportimportstructmain.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <fstream>
#include "exportimportstructdec.h"
using namespace std;

struct grades
{
	int grade1;
	int grade2;
	int grade3;
	int grade4;
}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
	{
		cout<<menu;
		cin>>choice;
		//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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90


#include <iostream>
#include <fstream>
#include "exportimportstructdec.h"
using namespace std;

struct grades;
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 << "\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));
			structArraySize++;
		}
		//display memory on screen:
		printmemory(structArraySize,pStructi);
	}
	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;
	scanstruct(pStructo);
	if (fileo.is_open())
	{
		fileo.write (reinterpret_cast<char*>(pStructo), sizeof(structo));
		fileo.close();
	}
	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;
	scanstruct(pStructa);
	if (filea.is_open())
	{
		filea.write (reinterpret_cast<char*>(pStructa), sizeof(structa));
		filea.close();
	}
	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";
	}
}


exportimportstructdec.h
1
2
3
4
5
6
7
8
9
10
11
12
#ifndef EXPORTIMPORTSTRUCTDEC_H
#define EXPORTIMPORTSTRUCTDEC_H 

struct grades;

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

#endif 
You should use header files to store classes and function prototypes, surround by the #ifndef FILE_H tags.

Source files should contain the function definitions.

Example:
Class.h
1
2
3
4
5
6
7
8
#ifndef CLASS_H
#define CLASS_H
class MyClass {
    int a;
    double b;
    void Func();
};
#endif 


Class.cpp
1
2
3
4
void MyClass::Func() {
    this->a = 1;
    return;
};
Last edited on
thank u all very much, i now understand how to
compile a vial pile of files in style

heres the final code if anyones interested (well its not final, ive got some work to do on making this more generic):

exportimportstructmain.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#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:
	do
	{
		cout<<menu;
		cin>>choice;
		//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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#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();
		filecheck.close();
		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));
			structArraySize++;
		}
		//display memory on screen:
		printmemory(structArraySize,pStructi);
	}
	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;
	scanstruct(pStructo);
	if (fileo.is_open())
	{
		fileo.write (reinterpret_cast<char*>(pStructo), sizeof(structo));
		fileo.close();
	}
	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;
	scanstruct(pStructa);
	if (filea.is_open())
	{
		filea.write (reinterpret_cast<char*>(pStructa), sizeof(structa));
		filea.close();
	}
	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";
	}
}

exportimportstructdec.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef EXPORTIMPORTSTRUCTDEC_H
#define EXPORTIMPORTSTRUCTDEC_H 

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 * );

#endif 
Topic archived. No new replies allowed.