File Handling

the read function of my Cell management class does not read from the file rather it gives[ garbage values. Please tell me if i'm doing something wrong here.
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
112
113
114
#include<iostream>
#include<stdlib>
#include<fstream>
#include<string>
using namespace std;

class Prison{
protected:
	string name;
   string address;
public:
	virtual void set()
   {
   cout<<"Enter name of the Prison";
   getline(cin,name);
   cout<<"Enter address of the prison";
   getline(cin,address);

   }

   virtual void show()
   {
   cout<<"\nPrison:"<<name;
   cout<<"\nAddress:"<<address;

   }


};

class Cellmanagement:public Prison
{
protected:
	int totalcells;
	int functionalcells;
   int emptycells;
	int srno;

public:
	void set()
	{
   	Prison::set();
		cout<<"enter total cells:   ";
		cin>>totalcells;
		cout<<"enter functional cells:   ";
		cin>>functionalcells;
		cout<<"enter empty cells:   ";
	   cin>>emptycells;
	}

   void show()
   {
   Prison::show();
   cout<<"\nTotal Cells"<<totalcells;
   cout<<"\nEmpty Cells"<<emptycells;
   cout<<"\nFunctional Cells"<<functionalcells;
   cout<<"\nOccupied Cells"<<occupiedcells();
   cout<<"\nNon-functional Cells"<<nonfunctionalcells();
   }
   int occupiedcells()
	{
   int temp;
	temp=functionalcells-emptycells;
	return temp;
	}
   int nonfunctionalcells()
	{
   int temp;
	temp=totalcells-functionalcells;
	return temp;
	}


   void write()
   {
   Cellmanagement cm;
   ofstream outfile("Cell management.txt",ios::app|ios::out);
   if(!outfile)
   {
    cout<<"error openning file"<<endl;
   }
   cm.set();
   outfile.write(reinterpret_cast<char*>(&cm),sizeof(cm));
   }
   void read()
   {
   Cellmanagement cm;
   ifstream infile("Cell management.txt",ios::in|ios::binary);
   if(!infile)
   {
   cout<<"Error openning file"<<endl;
   }
	infile.seekg(0);
   cm.show();
   infile.read(reinterpret_cast<char*>(&cm),sizeof(cm));

   }

};// end of cell managemnt class

void main()
{

  
  Cellmanagement c;
  
  cout<<"\n*****writing**************\n";
   c.write();
  cout<<"\n*****reading**************\n";
    c.read();
system("pause");

}
You cannot write complex data to a file. Your class 'Prison' contains a string which contains a pointer. So you store the pointer itself which is invalid the next time a change is made. Hence you have garbage.

Why not using the << and >> operator of the stream? You can extend that operator so that you can write somthing like cout << c; where 'c' is Cellmanagement
i don't get you. i tried overloading the stream operators as you said but it's still the same.
As coder777 said, you can't write references (pointers). Pointers refer to memory locations in the program, which usually have no meaning on subsequent executions of the program. Strings typically contain pointers, and look something like this:

class string
{
int len;
char *buf;
};

So when you write a string to a file, you may write its length and a pointer to the data that makes up the actual string, you won't write the string itself. The pointer can't be used to reconstitute the string when the read from a file. What the other poster was suggesting is to stream each member. Instead of:

outfile.write(...);

You do something like:

outfile << cm;

But you have to add operators to define this properly:

ostream& operator << (ostream& stream, const Prison& p)
{
stream << p.name << p.address;
return stream;
}

ostream& operator << (ostream& stream, const Cellmanagement& cm)
{
stream << (const Prison&)cm;
stream << cm.totalcells << cm.functionalcells << cm.emptycells << cm.srno;
return stream;
}

All of the builtin types have stream operators already defined, as does a string. You'll have to write corresponding operators to take the data out of the stream.

Hope this helps.
Topic archived. No new replies allowed.