Hi folks, I had created a class for student. This is the class.

class student
{
  int rollno,marks;
  char name[20];
 
 
 public:
    void show();
    void get();
    int filecreate(const string &,fstream &);
    int fileopen(const string &,fstream &);
    static int add(const student &,fstream &);
    static int read(const student &,fstream &,int);
    static int update(const student &,fstream &,int);
    int showall(fstream &);
    int view(fstream &,int );
    int adddummy(fstream &);
};

This is my add method to write a record in the binary file.

int student::add(const student &s,fstream &fp)
{
        fp.seekp(0, ios::end);
        if(fp.write((char*)&s,sizeof s))
            return 1;
        return 0;
}

If i write the values of the object s, What are the values written to the file. Is it only rollno, marks and name (Data members only ?). Now i want to add a member for fstream. after adding the member, if i write the file, what data will write into the file (will the fstream values also write to file?)

dumping of object directly into a file, will save member variables into stream.
So the roll number, marks and name will be dumped. The memory layout of the object will get reflected in file also. So you can check memory view of object, in the same way it will be dumped.
But writing object like this is not a good practice as reading of object requires lots of maintenance. Since a little change in your student class will impact reader code also.
Your last question is not clear to me, can you elaborate more on that part.

Your last question is not clear to me, can you elaborate more on that part.

Now the class has three members(roll no,marks and name). Ok. Now i added a new member for fstream (to open a file using constructor function). Now i want to write the file using that add() method. If i write the student object into the file, the value of the object of the fstream also will write into the file ????

But writing object like this is not a good practice as reading of object requires lots of maintenance

So You are telling me to create a separate class for student file ?

So You are telling me to create a separate class for student file ?

no, he's telling you to not blindly serialise objects at all.
Instead define a well documented file format containing the fields you want in the order you want, and create readers and writers for that format producing and consuming instances of the class (methods that may or may not be members of the class).

no, he's telling you to not blindly serialise objects at all.
Instead define a well documented file format containing the fields you want in the order you want, and create readers and writers for that format producing and consuming instances of the class (methods that may or may not be members of the class).

Am not well in English. Give me a very simple example code to implement the concept as you said above. I know that i should not expect a code from this forum. But i need it to implement soon . If you can, Just give me a very simple example

He said define the data layout. Add methods to load and retrieve the data elements. Add methods to read and write the data elements.

Try this:
Make sure NAME is your first data element.
Write the class
Immediately write the string "<END OF CLASS>"
Examine at the file in binary.

Make your changes to the class.
Do the above again.
Compare the first file to the second.
If the files are identical, the changes do not affect your file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.