plz help me with virtual functions file handling code

virtual functions and file handling

Page 1 of 1

8 Replies - 580 Views - Last Post: 08 April 2010 - 11:50 AM Rate Topic: -----

#1 fireblade2010  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-April 10

plz help me with virtual functions file handling code

Posted 08 April 2010 - 07:40 AM

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 acadmic:public 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::putdata()
{
	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::out);//|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

This post has been edited by JackOfAllTrades: 08 April 2010 - 08:57 AM
Reason for edit:: Added code tags.


Is This A Good Question/Topic? 0
  • +

Replies To: plz help me with virtual functions file handling code

#2 Munawwar  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 162
  • View blog
  • Posts: 457
  • Joined: 20-January 10

Re: plz help me with virtual functions file handling code

Posted 08 April 2010 - 08:38 AM

Since you are reading and writing binary data, you must use ios::binary (for both reading and writing).

EDIT: And btw, if you are using Borland 3.1 IDE then I suggest that you get a more modern compiler/IDE.

This post has been edited by Munawwar: 08 April 2010 - 08:41 AM

Was This Post Helpful? 0
  • +
  • -

#3 jbedo465  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 18-February 10

Re: plz help me with virtual functions file handling code

Posted 08 April 2010 - 08:40 AM

Please post your code in code blocks.
Was This Post Helpful? 0
  • +
  • -

#4 fireblade2010  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-April 10

Re: plz help me with virtual functions file handling code

Posted 08 April 2010 - 09:36 AM

i tried doing it

still problem persists

plz help me with the function call in case 2
Was This Post Helpful? 0
  • +
  • -

#5 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1875
  • View blog
  • Posts: 20,284
  • Joined: 17-March 01

Re: plz help me with virtual functions file handling code

Posted 08 April 2010 - 09:38 AM

Please is spelled "p l e a s e" not with a "z"
Was This Post Helpful? 0
  • +
  • -

#6 fireblade2010  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-April 10

Re: plz help me with virtual functions file handling code

Posted 08 April 2010 - 09:41 AM

ok i am sorry

please will u help me now
Was This Post Helpful? 0
  • +
  • -

#7 Munawwar  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 162
  • View blog
  • Posts: 457
  • Joined: 20-January 10

Re: plz help me with virtual functions file handling code

Posted 08 April 2010 - 10:34 AM

Seems to work for me with ios::binary. Delete stud.txt and make an empty stud.txt. Then run the program.
And also write -
...
while(file.read((char *)ptr,sizeof(a)))
{
...


instead of
...
while(file)
{
   file.read((char *)ptr,sizeof(a));
....
}


Why? Read this

And also consider quitting Borland 3.1 since its too old. Most people can't even compile your code.
Was This Post Helpful? 0
  • +
  • -

#8 fireblade2010  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-April 10

Re: plz help me with virtual functions file handling code

Posted 08 April 2010 - 10:48 AM

try compiling this code then

it runs but when u enter into subsequent case it is failing...

please help...

code is in attachment

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdlib.h>
#include<Stdio.h>

class student
{
	protected:

	int rollno;
	char name[50],degree;

	public:

	void virtual getdata()=0;
	void virtual putdata()=0;
	void virtual calresult()=0;
	int virtual checkroll(int)=0;
};


class acadmic:public student
{
	protected:

	int imk1,imk2,emk1,emk2;
	float result;

	public:

	void getdata();
	void putdata();
	void calresult();
	int checkroll(int);
};

int acadmic::checkroll(int r)
{
	if(rollno==r)
	return 1;
	else
	return 0;
}

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::putdata()
{
	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.dat",ios::in|ios::out|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'://>/insert

			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.read((char *)ptr,sizeof(a)))
				ptr->putdata();
				//a.putdata();


			file.clear();
			break;

			case '3': //searching
			int t_rn,flag;
			cout<<"\nEnter roll No. to be Searched:";
			cin>>t_rn;
			file.seekg(0,ios::beg);

			while(file.read((char *)ptr,sizeof(a)))
			{
				flag=ptr->checkroll(t_rn);
				//flag=a.checkroll(t_rn);
				if(flag)
				{
					cout<<"\n Record Found...";
					ptr->putdata();
					//a.putdata();
					break;
				}
			}
			if(!flag)
			cout<<"\nRecord Not found....";
			file.clear();
			break;

			case '4': //modify

			cout<<"\nEnter roll No. to be Modified:";
			cin>>t_rn;
			file.seekg(0,ios::beg);

			while(file.read((char *)ptr,sizeof(a)))
			{
				flag=ptr->checkroll(t_rn);
				//flag=a.checkroll(t_rn);
				if(flag)
				{
					ptr->getdata();
					ptr->calresult();
					//a.getdata();
					//a.calresult();
					file.seekp(-67,ios::cur);
					file.write((char *)ptr,sizeof(a));
					break;
				}
			}
			if(!flag)
			cout<<"\nRecord Not found....";
			file.clear();
			break;

			case '5': //delete

			fstream file2("stud1.dat",ios::in|ios::out|ios::binary);

			cout<<"\nEnter roll No. to be Deleted:";
			cin>>t_rn;
			file.seekg(0,ios::beg);

			while(file.read((char *)ptr,sizeof(a)))
			{
				flag=ptr->checkroll(t_rn);
				//flag=a.checkroll(t_rn);
				if(!flag)
				{
					file2.seekp(0,ios::beg);
					file2.write((char *)ptr,sizeof(a));
				}
			}

			file.close();
			file2.close();
			remove("stud.dat");
			rename("stud1.dat","stud.dat");
			file.open("stud.dat",ios::in|ios::out|ios::binary);
			file.clear();
			break;

		}
		cout<<"\n Do you want to continue?";
		ans=getche();
	}while(ans=='y'||ans=='Y');
	file.close();
	cout<<"\nFile closed Successfully...";
}



Admin Edit: Please use code tags when posting your code. Code tags are used like so => :code:

Thanks,
PsychoCoder :)

This post has been edited by PsychoCoder: 08 April 2010 - 10:49 AM

Was This Post Helpful? 0
  • +
  • -

#9 Munawwar  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 162
  • View blog
  • Posts: 457
  • Joined: 20-January 10

Re: plz help me with virtual functions file handling code

Posted 08 April 2010 - 11:50 AM

fstream file2("stud1.dat",ios::in|ios::out|ios::binary); wont create a file

Even fstream file2("stud1.dat",ios::out|ios::binary); wont work - cause its Borland 3.1. Heh!.
You cannot create a file using fstream in Borland 3.1. You *have* to use ofstream for creating.
You need to do ofstream file2("stud1.dat",ios::binary);

And second logical error is in the case '5' (Check comments)
...
//Inside while loop
 if(!flag)
 {
    file2.seekp(0,ios::beg); //Why do you need this here?
    file2.write((char *)ptr,sizeof(a));
 }
...


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1