saving Objects on binary file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • asoom
    New Member
    • Feb 2013
    • 4

    saving Objects on binary file

    Can any one help me to fix this code? the result is Garpish>>

    Code:
    <iostream>
    #include<fstream>
    using namespace std;
    class Student
    {
    public:
    	char name[10];
    	char address[10];
    	char Gender;
    	char DOB[10];
    	Student()
    	{}
    };
    int main()
    {
    	cout<<"\nWritting on file \n";
    		Student *p=new Student[5];
    			for(int i=0;i<5;i++)
    	{
    		cout<<i+1<<": ";
    		cin>>p->name;
    		cout<<"\n";
    		p++;
    		}
    	ofstream osfile("Student.txt",ios::binary|ios::app);
    	osfile.write((char*)p,sizeof(Student)*5);
    osfile.close();	
    
    	cout<<"\nreading\n";
    	Student *p2=new Student[5];
    
    	ifstream isfile("Student.txt",ios::binary);
    	isfile.read((char*)p2,sizeof(Student)*5);
    	isfile.seekg(0);
    	isfile.close();
    for(int i=0;i<5;i++)
    	{
    		cout<<i+1<<": ";
    		cout<<p2->name;
    		cout<<"\n";
    		p2++;
    		}
    
    	return 0;
    }
    Last edited by acoder; Feb 12 '13, 01:18 AM.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    There's a lotta stuff wrong here. The big thing is you are incrementing p and p2. These variables contain the addresses of your arrays which you lose by incrementing these variables.

    Use another variable to do the increment.

    I would be writing two programs here. One to do the file input/outut and the other to implement your Student. Get the file working then add the Student and verify things still work. Then make the data private and write member functions to set and fetch the data Verfy things are still working.

    I think you are trying to do too much at one time here. Take it in steps.

    Comment

    • asoom
      New Member
      • Feb 2013
      • 4

      #3
      Thanks weaknessforcats :)

      it works with me effeciently by applying your statment:
      (Use another variable to do the increment.)


      here's the code :)
      Code:
      #include<iostream>
      #include<fstream>
      using namespace std;
      class Student
      {
      public:
      	char name[10];
      	char address[10];
      	char Gender;
      	char DOB[10];
      	Student()
      	{}
      };
      int main()
      {
      	cout<<"\nWritting on file \n";
      		Student *p=new Student[5];
      		Student *ptr=p;
      			for(int i=0;i<5;i++)
      	{
      		cout<<i+1<<": ";
      		cin>>ptr->name;
      		
      		cout<<"\n";
      		ptr++;
      		}
      	ofstream osfile("Student.txt",ios::binary|ios::app);
      	osfile.write((char*)p,sizeof(Student)*5);
      osfile.close();	
      
      	cout<<"\nreading\n";
      	Student *p2=new Student[5];
      
      	ifstream isfile("Student.txt",ios::binary);
      	isfile.read((char*)p2,sizeof(Student)*5);
      	//isfile.seekg(0);
      	isfile.close();
      for(int i=0;i<5;i++)
      	{
      		cout<<i+1<<": ";
      		cout<<p2->name;
      		cout<<"\n";
      		p2++;
      		}
      
      	return 0;
      }
      Last edited by Rabbit; Feb 11 '13, 11:34 PM. Reason: Please use code tags when posting code. This is your second warning.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        You are still modifying p2. Remember, you have allocated the p and p2 arrays. You need to delete these arrays and you can't do that of the address in either p or p2 has been changed

        But after you fix that, make a copy of this code and save it somewhere.

        Then, make the data members in Student private.

        Then, write member functions to assign/retrieve the data members.

        Then use these functions.

        Maybe like:

        Code:
        char arr[10];
        con >> arr;
        p[0].SetName(arr);
        etc...
        cout << p2[0].GetName();
        etc...
        If you start with working code, after these modifications you should still have working code.

        Comment

        • asoom
          New Member
          • Feb 2013
          • 4

          #5
          1)Should i need another ptr when i read once from the file?

          2)u said:"You need to delete these arrays and you can't do that of the address in either p or p2 has been changed"
          -> then u mean i willn't delete these array since there addresses
          have been changed?

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Your p2 pointer to an array for reading is OK. What's not OK is this:
            Code:
            for(int i=0;i<5;i++)
             39.     {
             40.         cout<<i+1<<": ";
             41.         cout<<p2->name;
             42.         cout<<"\n";
             43.         p2++;             <-----!!!!!!
             44.         }
            When you allocate an array with the new operator, you need to delete the aloocation when you are finished with it.
            Like this:

            Code:
            delete [] p2;
            However, if the address in p2 is not the one the new operator gave you, your program will crash on the delete.

            You should handle the p2 array the same way as you did the p array.

            Comment

            • asoom
              New Member
              • Feb 2013
              • 4

              #7
              Thanks weaknessforcats ;
              really u are Collaborator person :)

              Comment

              Working...