Structs can be writed in fail directly, i was try it.But when I try to do the same with class , i can't after read corectly the data in fail.
Did some one know how to write whole class(not member by member) in fail.
In the moment i can't post the real code but it likes some like that:
class a{
int c;
char b[];
public:
void write();
void read();
}n[2];
the function is like that:
void a::write()
{
fp.open("f.doc",ios::binary|ios::out);
if(!fp)
{cout<<endl<<" Error";
exit(1);
}
fp.write(n,sizeof(n));//I'm not sure in the moment is write had another arguments
fp.close();
}
It works I open it with notepad and the data is there. But if I try to read it with this it read uncorrectly.
void a::read()
{
fp.open("f.doc",ios::binary|ios::out);
if(!fp)
{cout<<endl<<" Error";
exit(1);
}
fp.read((char*)&n,sizeof(n));//I'm not sure in the moment is read had another arguments
fp.close();
}
Well, in the code above you are referring to "n" which is the object's name, not the data inside the object. You mean to be writing to b. And sizeof(b) will only work for a statically allocated array.
But assuming those are just "typos", your problem seems to be this:
You might want to enable exceptions on your file stream. The exception rules for fstreams seemed a bit odd to me at first, but I have gotten used to them. I certainly prefer using exceptions with fstreams() to constantly checking for error conditions.
the errors in the code is becouse I'm forgot what exactly was writen. But i know that the programs runs(I don't think that with these mine errors it will be runs). But the true code(without errors) work if class is changed with struct,(read and write), but with class only write and read wrongly.
Code like this(may be have error becouse is just remember. In the moment i can't compile it and clear errors but the way of it must be work will be clear i think)[code
]struct a{
int c;
char b[];
void write();
void read();
}n[2];
So you're trying to do binary object serialization. In that case your code is very wrong. Presumably the reason it "works" (or at least you think it does) with a struct and not a class is an implementation issue.
In general it is useless to post guesses at what your code might be. Post your actual code, as a running program, with headers and a main. Otherwise we are just guessing too.