Need help writing class to binary file.

Hello, im trying to write a class into a binary file, although i am getting some errors when i try putting in information.

I have 3 files, a driver, header , and the main 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111

Header:

#ifndef RECORDFILE_H
#define RECORDFILE_H
#include <string>

using namespace std;

//****************************************************************************************************

class Records
{
private:
    string  studentName;
    double *quiz;
	double *assignment;
	int weeks;

public:
	void load(ifstream& inf); 
	void store(ofstream& of);
	Records ();
	void setData ( string, int, double, double);
    ~Records ();

};

#endif


Main:

#include "RecordFile.h"
#include <string>
#include <iostream>

//****************************************************************************************************

Records::Records ()
{
    studentName = " ";
    weeks   = 0;
	quiz = 0;
	assignment = 0;
}
//****************************************************************************************************

void Records::setData( string name, int size, double q[], double a[])
{

	studentName = name;
	weeks = size;
	quiz = new q[];
	assignment = new a[];

}
//****************************************************************************************************


void Records::store(ofstream& of)
{
  of.write(&studentName, sizeof(studentName));
  of.write(&weeks, sizeof(weeks));
  of.write(&quiz, sizeof(quiz));
  of.write(&assignment, sizeof(assignment));
} 
//****************************************************************************************************

void Records::load(ifstream& inf)
{
  inf.read(&studentName, sizeof(studentName));
  inf.read(&weeks, sizeof(weeks));
  inf.read(&quiz, sizeof(quiz));
  inf.read(&assignment, sizeof(assignment));
}
 
//****************************************************************************************************
Records::~Records()
{
   
}
//****************************************************************************************************

Driver:


#include "RecordFile.h"
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>

using namespace std;

//****************************************************************************************************

int main()
{


  setData Record("Me", 2, 4, 5);
  ofstream myfile;
  myfile.open("Records.dat", ios::binary | ios::out);
  Record.store(myfile);
  myfile.close();
  return(0); 


}


i received this error: error C2228: left of '.read' must have class/struct/union
C2027: use of undefined type 'std::basic_ifstream<_Elem,_Traits>'
Thanks for the help.
Last edited on
You forgot to include the fstream library in your header file.

By the way, this probably won't work because you are using strings (variable size) instead of character arrays (of fixed size).
Last edited on
Ah ok, i added that in.

Any tips how i could convert it to a character array to make it work?
I was wrong about what problem I thought you would have in using strings, though there is still a problem:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>

int main()
{
    std::string empty;
    std::string blank = "";
    std::string stuff = "stuff";
    std::string something = "something";
    std::cout << "(empty):   " << sizeof(empty) << '\n';
    std::cout << "(blank):   " << sizeof(blank) << '\n';
    std::cout << "stuff:     " << sizeof(stuff) << '\n';
    std::cout << "something: " << sizeof(something) << '\n';
}
(empty):   8
(blank):   8
stuff:     8
something: 8

So you'd still need to use character arrays.

As for how you use them, just replace line 15 with:
char studentName[100]; //or whatever number you think is long enough
and relevant parts of lines 63 and 72 with
(studentName, 100); //or whatever number you used

Also, you may have to change other parts of your code since it is now a cstring, not a string.
Topic archived. No new replies allowed.