Thread: Writing array of objects in a file

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jan 2011
    Location
    India
    Posts
    9

    Writing array of objects in a file usyng write()

    I am having trouble writing and reading array of objects.
    this program is representing the trouble that I am having in real one.
    I want to write class objects in a file "Stu.txt" and then read it so that i can display its contents the program is giving garbage value after 3 objects.
    i don't know why? I have search through net but can't find any solution.
    I don't have any idea about the problem.

    I am using borland 5.0.2 in windows xp sp2
    And one more thing why the contents of Stu.txt are displayed in form of symbols
    that too only values of those variables which are not char?
    I have provided the output just in case.
    Please help me...
    Thanks in advance.

    Here is my code.


    Code:
    #include <fstream.h>
    #include <iostream.h>
    #include <stdio.h>
    #include <windows.h>
    
    //definition for class student
    class Student {	char name[10];
    						char grade;
    						float marks;
                      int rn;
    				public :
    					void getdata()
    					{	cout<<"\nenter name "; gets(name);
    						cout<<"enter grade ";	cin>>grade;
    						cout<<"enter marks ";	cin>>marks;
                      cout<<"enter roll no ";	cin>>rn;
    					}
    
    					void display()
    					{	cout<<"\nName ";puts(name);
    						cout<<"Grade "<<grade;
    						cout<<"\nMarks "<<marks;
                      cout<<"\nroll no "<<rn;
    					}
    				 };
    
    	int main()
    		{
    			Student arts[5];
    			fstream filin;            
    //defining fstream objects
              filin.open("Stu.txt",ios::in|ios::out);  
    //opening the file Stu.txt
    			if(!filin)                
    //if filin is zero
    			{
    				cout<<"\ncannot open file.";
    				return 1;
    			}
    			cout<<"\nEnter details of students";        
    //Entering data
    			for(int i=0;i<5;i++)
    			{	arts[i].getdata();
    				filin.write((char*)&arts[i],sizeof(arts[i]));      
     //writing in Stu.txt
    			}
    			filin.close();    
     //closing file Stu.txt
    			filin.open("Stu.txt",ios::in|ios::out);  
    //opening file Stu.txt
    			if(!filin)     
    //checking again for filin
    			{
    				cout<<"\ncannot open file.";
    				return 1;
    			}
    			filin.seekg(0);
    			cout<<"\nthe contents of stu.txt are ";
    			for(int i=0;i<5;i++)
    			{	filin.read((char*)&arts[i],sizeof(arts[i]));	
    //reading from file
    				arts[i].display();
    			}
    			filin.close();     
    //closing file Stu.txt
             system("pause"); 
    /*for pause progam as the program
                                automatically exits which enables
                                user to see results*/
    		}
    Output

    enter name ay
    enter grade A
    enter marks 41
    enter roll no 1

    enter name ak
    enter grade A
    enter marks 42
    enter roll no 2

    enter name am
    enter grade A
    enter marks 43
    enter roll no 3

    enter name af
    enter grade A
    enter marks 44
    enter roll no 4

    enter name as
    enter grade A
    enter marks 45
    enter roll no 5

    the contents of stu.txt are

    enter name ay
    enter grade A
    enter marks 41
    enter roll no 1

    enter name ak
    enter grade A
    enter marks 42
    enter roll no 2

    enter name am
    enter grade A
    enter marks 43
    enter roll no 3

    enter name af
    enter grade
    // here are the errors - no grade
    enter marks 2.28266e-36
    // garbage value
    enter roll no 1627389952
    // garbage value

    enter name s
    // missing a in name as
    enter grade
    // missing grade
    enter marks 9.13139e-36
    // garbage value
    enter roll no 0
    // garbage value
    Last edited by Salem; 01-12-2011 at 01:52 AM. Reason: Use [code][/code] tags for posting CODE!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing a text file to an array?
    By Raen in forum C Programming
    Replies: 11
    Last Post: 10-15-2010, 06:26 PM
  2. Can you help me about tolower() in file
    By nctar in forum C Programming
    Replies: 7
    Last Post: 05-12-2010, 10:04 AM
  3. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  4. Writing an array of struct to file
    By stellastarr in forum C Programming
    Replies: 10
    Last Post: 03-25-2006, 06:59 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM