How to write in fail class

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.
closed account (z05DSL3A)
Sorry, I don't understand. Can you post some code to demonstrate the problem?

It sounds like you are having trouble with Access Levels. struct has public Access level by default, class has private.
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:

fp.open("f.doc",ios::binary|ios::out);

You want ios::in for input!

This is also wrong:

fp.read((char*)&b,sizeof(b));

You really just want this:

fp.read(b,sizeof(b));
Last edited on
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];

void a::write()
{
fp.open("f.doc",ios::binary|ios::out);
if(!fp)
{cout<<endl<<" Error";
exit(1);
}
fp.write((char*)n,sizeof(n));
fp.close();
}


void a::read()
{
fp.open("f.doc",ios::binary|ios::in);
if(!fp)
{cout<<endl<<" Error";
exit(1);
}
fp.read((char*)&n,sizeof(n));
fp.close();
}

//in true it must bework for structs but not with classes


[/code]
closed account (z05DSL3A)
If you want to change a struct, such as:
1
2
3
4
5
6
struct a{
    int c;
    char b[];
    void write();
    void read();
}

to a claass, you will have to change the access level to the relevant members, such as:
1
2
3
4
5
6
7
class a{
    int c;
    char b[];
public:
    void write();
    void read();
}


See: http://www.cplusplus.com/doc/tutorial/classes/
Yes i do this. I know that classes is private by default
I think the thing you are missing most is actually: how to do binary I/O with an object.

See http://www.google.com/search?q=c%2B%2B+serialization

Then, write a friend function to overload the << operator, or just write a class member function, or both, to handle doing the I/O.

Hope this helps.
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.
Topic archived. No new replies allowed.