save and load class to binary file

Hi guys, i recently start to learn abt the class and now try to save and load the class in binary file.. i was trying to do 2 program, 1 was insert the class into a binary file, the second one is to load the class from the previous binary file and use it, but some problem arise..
this is 1st code (save data):
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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int streetnum=10;
struct house
{
       int no;
       string newspaper[5];
       double price[5];
};
class street
{
      string streetname;
      house housedetail[10];
      public:
             street (){};
             street (string a, house b[], int size)
             {
                    setstreetname(a);
                    sethouse(b, size);
             }
             void setstreetname (string a)
             {    streetname=a; }
             void sethouse (house a[], int size)
             {    
                  for (int i=0; i<size; i++)
                  housedetail[i]=a[i];
             }
             string getstreetname()
             {      return streetname;  }
};
int main ()
{
    fstream usefile( "data.dat",  ios::out | ios::binary );
    if ( !usefile )
    {
      cout << "File could not be opened." << endl;
    }
    house house1[5]={
          {31,{"Berita Harian    ","New Straits Times"},{1.4,1.3}},
          {32,{"Sin Chew Daily   "},{1.3}},
          {33,{"The Star         ","Sin Chew Daily   "},{1.3,1.3}}
          };
    street street1("Jalan Merah", house1, 5);
    
    usefile.seekp(0);
    usefile.write( reinterpret_cast<const char * >(&street1),sizeof(street));
    
    system ("pause");
    return 0;
}

My second code (load and use data):
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 <string>
using namespace std;
const int streetnum=10;
struct house
{
       int no;
       string newspaper[5];
       double price[5];
};
class street
{
      string streetname;
      house housedetail[10];
      public:
             street (){};
             street (string a, house b[], int size)
             {
                    setstreetname(a);
                    sethouse(b, size);
             }
             void setstreetname (string a)
             {    streetname=a; }
             void sethouse (house a[], int size)
             {    
                  for (int i=0; i<size; i++)
                  housedetail[i]=a[i];
             }
             string getstreetname()
             {      return streetname;  }
};
int main ()
{
    fstream usefile( "data.dat", ios::in |ios::binary );
    if ( !usefile )
    {
      cout << "File could not be opened." << endl;
    }
    street street2;
    
    usefile.seekg(0);
    usefile.read( reinterpret_cast< char * >( &street2 ), sizeof( street ) );
    
    cout<<street2.getstreetname()<<endl;
    system("pause");
    return 0;
}


The 1st program run ok, but the second program cant run properly.. and it write something like access violation, segmentation fault.. i think it cant access the statement "cout<<street2.getstreetname()<<endl;" can any1 tell me where's my mistake pls? thank you...

p/s: i try to combine the two program, which it save the data and load the data in same program, that can run successfully..

What you are trying to do is in violation of the C++ standard, and that is why it doesn't work.

Line 43 is essentially a "memcpy" of the class to disk. The standard specifically states that this can only be done for POD-types and simple aggregates, where a simple aggregate is a class or struct that contains no destructors, no constructors, no member functions (virtual or not), and contains only POD-types.

Your class contains a string which is not a POD type (which is really the reason for the crash) and also member functions.

To do what you want to do, you need to write each individual member of the class to disk, keeping in mind that string itself is not a simple aggregate.
i try to change the string type to cstring and now the program run successfully... thank for the help and info...
Topic archived. No new replies allowed.