-
April 8th, 2010, 09:48 AM
#1
plz help me with virtual functions file handling code
here is the code
plz help me with the case 2 present in main...
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdlib.h>
class student
{
protected:
int rollno;
char name[50],degree;
public:
void virtual getdata()=0;
void virtual putdata()=0;
void virtual calresult()=0;
};
class acadmicublic student
{
protected:
int imk1,imk2,emk1,emk2;
float result;
public:
void getdata();
void putdata();
void calresult();
};
void acadmic::getdata()
{
cout<<"\n Enter Roll No:";
cin>>rollno;
cout<<"\n Enter Name:" ;
cin>>name;
cout<<"\n Enter Course:";
cout<<"\n E for Enginnering \n M for MCA:";
cin>>degree;
cout<<"\n Enter Internal Marks for test1 and test 2:" ;
cin>>imk1>>imk2;
cout<<"\n Enter Marks For Extrenal Test1 and Test 2:";
cin>>emk1>>emk2;
}
void acadmic:utdata()
{
cout<<"\n Roll No:"<<rollno;
cout<<"\n Name:"<<name;
cout<<"\n Course:"<<degree;
cout<<"\n Internal Marks";
cout<<"\n "<<imk1<<"\n "<<imk2;
cout<<"\n External Marks";
cout<<"\n "<<emk1<<"\n "<<emk2;
cout<<"\n The Result Is "<<result;
}
void acadmic::calresult()
{
result=(imk1+imk2+emk1+emk2)/4;
}
void main()
{
student *ptr;
acadmic a;
ptr=&a;
fstream file;
file.open("stud.txt",ios::in|ios:ut);//|ios::binary);
if(file==NULL)
{
cout<<"\nFile opening error....";
getch();
exit(1);
}
char ans;
do
{
clrscr();
cout<<"\n******** MENU ********\n\t1.Insert\n\t2.Display\n\t3.Search\n\t4.Modify\n\t5.Logical Delete\nEnter your choice?";
switch(getche())
{
case '1':
file.seekp(0,ios::end);
ptr->getdata();
ptr->calresult();
file.write((char *)ptr,sizeof(a));
file.clear();
break;
case '2': //DISPLAYING
file.seekg(0,ios::beg);
while(file)
{
file.read((char *)ptr,sizeof(a));
ptr->putdata();//wat is the reason that it ddnt work
//a.putdata(); //even this call works
}
//ptr->putdata(); //this works alone
file.clear();
break;
}
cout<<"\n Do you want to continue?";
ans=getche();
}while(ans=='y'||ans=='Y');
file.close();
cout<<"\nFile closed Successfully...";
}
thanks
Nilesh
-
April 8th, 2010, 10:00 AM
#2
Re: plz help me with virtual functions file handling code
Please use code tags for posting code.
Code:
file.read((char *)ptr,sizeof(a));
You are casting a class pointer to a char pointer. What do you think will happen ? Also, sizeof(a). You are trying to take the size of a class. DON'T.
-
April 8th, 2010, 11:35 AM
#3
Re: plz help me with virtual functions file handling code
we always have to cast class pointer to char pointer while reading or writing to a file
and also need to specify size of the object which i did
plz tell me whats wrong with that function call
-
April 8th, 2010, 11:52 AM
#4
Re: plz help me with virtual functions file handling code
we always have to cast class pointer to char pointer while reading or writing to a file
But what do you expect to happen ? You have a pointer to a class and you are telling the compiler that it is a character buffer. Do you expect to write the class to disk ? You can't write a class to disk. You can only write the data in the class members to disk (for example the 'name' and 'degree') but you need them to write them individually.
So this..
Code:
file.write((char *)ptr,sizeof(a));
and this...
Code:
file.read((char *)ptr,sizeof(a));
is incorrect.
This...
Code:
file.write(ptr->name,50);
writes the name (50 chars) to disk.
-
April 8th, 2010, 11:59 AM
#5
Re: plz help me with virtual functions file handling code
but i want to write entire object
and not the individual fields
-
April 8th, 2010, 12:02 PM
#6
Re: plz help me with virtual functions file handling code
You cannot do that. If it's a class, you must write each member individually. Philip has already said this in your other (duplicate) post:
http://www.codeguru.com/forum/showthread.php?t=495486
Viggy
-
April 8th, 2010, 12:14 PM
#7
Re: plz help me with virtual functions file handling code
ok
i got what u r trying to explain
now problem is since i am writing in file from main() hence i cant write each data member individually
can u suggest any other approach
thank you
-
April 8th, 2010, 12:52 PM
#8
Re: plz help me with virtual functions file handling code
now problem is since i am writing in file from main() hence i cant write each data member individually
Why not ? What have the main function and writing to a file have to do with each other ? You can write to a file from every function you want.
-
April 8th, 2010, 01:40 PM
#9
Re: plz help me with virtual functions file handling code
thanks a lot sir
i finally found a solution for my problem
thanks a lot
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|