1

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

miken32
  • 42,008
  • 16
  • 111
  • 154
Heartly
  • 75
  • 1
  • 8

3 Answers3

3

I think what you want to achieve in the first place is serialization of the class instance.

This question and its answers may be helpful for this purpose. Especially this link (provided in the highest rated answer there) to a FAQ on serialization in C++ may be worth considering for you.

Community
  • 1
  • 1
moooeeeep
  • 31,622
  • 22
  • 98
  • 187
2

What are the values written to the file. Is it only rollno, marks and name (Data members only ?).

In your case - yes, because the class has no vtable or parents.

if i write the file, what data will write into the file (will the fstream values also write to file?)

Yes. fstream values will be completely pointless to you, but they will be written. However, if you will read the stored value the same way, the object will become unusable, because fstream may contain handles, pointers etc. - data which will not be valid on the second run.

This method also has binary compatibility problems - every time you add or change class members, your previously saved data becomes unusable.

Lyth
  • 2,171
  • 2
  • 29
  • 37
1

I am not sure to understand your question, but if you add a data member fstream foo; after name in your class student that member will be written also. However, writing a fstream is usually meaningless, because (at least on Unix systems) opened files don't stay open after the process ends. You should ask yourself what will happen when you read again that data (and how will you read it). Reading an fstream which you have written this way is meaningless.

And writing binary data is usually a mistake when done by a newbie. The point is that binary data is very brittle. For instance, in your example, you might later want to grow the size of the name[20] to some higher value (e.g. name[32]), because you figure that some people have really long names. If you did that, and if you have written all the data for your school in binary form (when the application used name[20]), you won't be able to read it back (without coding conversion routines) with an application where name has grown to name[32].

This is why it is usually worthwhile to either use some higher level tool (e.g. a database like MySQL) or to represent long-lasting data in textual forms, using standards like XML or JSON or YAML. Of course it does have drawbacks: reading & writing data is more complex to code and slower to execute. But textual data is easier to debug, as a programmer you can view it with ordinary editors.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • 'textual data is easier to debug, as a programmer you can view it with ordinary editors' if i stored as textual data its very hard to update the record, and delete the record from the file. can i use random access method in text file? – Heartly Nov 02 '11 at 09:58
  • No, you cannot use easily random access on a text file. However, you could load all the text file in memory (which is feasible if your data is not huge) and then operate in memory and finally dump a textual file. You could also use databases or (in simple cases) indexed files like GDBM. But don't forget that binary data is brittle! – Basile Starynkevitch Nov 02 '11 at 10:04
  • No. We are not interest to use relational DB system. Our backend only flat files. What about ISAM? whether GDBM is a software or concept like hash table? – Heartly Nov 02 '11 at 10:18