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.
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
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:
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.