954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Doubt in writing in binary file.

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?)

infantheartlyje
Light Poster
29 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

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.

tajendra
Junior Poster in Training
53 posts since Mar 2010
Reputation Points: 34
Solved Threads: 7
 
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 ????

infantheartlyje
Light Poster
29 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 
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 ?

infantheartlyje
Light Poster
29 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 
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).

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 
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

infantheartlyje
Light Poster
29 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

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

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: