Dhiraj Tayade
Dhiraj Tayade

Reputation: 417

Reading and Writing a class data to binary file in VC++

class Student 
{
public:
Student *prev;  
char S_Name[15];
char F_Name[15];
int Reg_Num;
char Section;
char MAoI[15];
float CGPA;
Student *next;
} 

I have the above class and I want to write the data to a binary file of the link list when I exit the program and again read it back and form a link list when the program is run. I have tried but failed on several attempts !

The code for Entering data `Student * Add_Entry() { Student *temp=new Student();

char s_Name[15];
char f_Name[15];
int reg_Num;
char section;
char mAoI[15];
float cGPA;

cout <<"**********************Menu***************************\n ";
cout<<"\nEnter the Studets name \"";
cin>>s_Name;

cout<<"\nEnter Father`s name \"";
cin>>f_Name;

cout<<"\nEnter the Registration Number \"";
cin>>reg_Num;

cout<<"\nEnter the Section \"";
cin>>section;

cout<<"\nEnter the Major Area of Interest \"";
cin>>mAoI;

cout<<"\nEnter the Current CGPA \"";
cin>>cGPA;

strcpy_s(temp->S_Name,s_Name);
strcpy_s(temp->F_Name,f_Name);
temp->Reg_Num=reg_Num;
temp->Section=section;
strcpy_s(temp->MAoI,mAoI);
temp->CGPA=cGPA;
temp->next=NULL;
temp->prev=NULL;
return temp;

//temp=Create_node(s_Name,f_Name,reg_Num,section,mAoI,cGPA);    

}`

To read from file i use ` char *buffer;

    ifstream reader;
    reader.open("student.bin",ios::in | ios::binary);
    if(reader.is_open)
    {
        do
        {
            reader.read(buffer,ch);
            if(Header==NULL)
            {
                Header=(Student)*buffer;
                temporary=Header;
            }

            else
            {
                temporary->next=(Student)*buffer;
                temporary=temporary->next;
            }
        }while(buffer!=NULL);
    }

`

And to write I use ` temporary=Header; //Backup Entries ofstream writer; writer.open("student.bin",ios::out | ios::binary);

                while(temporary!=NULL)
                {   
                    writer.write((char)* temporary,sizeof(temporary));
                    temporary=temporary->next;
                }

                writer.close();

`

Upvotes: 0

Views: 418

Answers (1)

JasonD
JasonD

Reputation: 16612

This line:

Header=(Student)*buffer;

Means: Take buffer, which is probably a pointer to char, and dereference it, to get a char. Then cast that char into a Student. The compiler has no idea how to convert a char into a Student.

If instead you do this:

Header= *((Student *)buffer);

It will cast the pointer to a pointer of the right type, and then dereference it to give a struct, which can be copied.

You do that all over the place.

Also, when reading, you don't fill in the "next" pointer for the final item, nor the "prev" pointer for any. Although the "next" pointer in the last saved item might be zero (assuming it was saved correctly), the prev pointers could point at anything. Best practice would be to initialise everything correctly.

Also:

if(reader.is_open)

should be:

if(reader.is_open())

Upvotes: 0

Related Questions