delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2004/09/04/14:37:22

X-Authentication-Warning: delorie.com: mail set sender to djgpp-bounces using -f
Message-ID: <20040904180941.51563.qmail@web20223.mail.yahoo.com>
Date: Sat, 4 Sep 2004 11:09:41 -0700 (PDT)
From: pradyut bhattacharya <pradyut100 AT yahoo DOT com>
Subject: file I/O
To: djgpp AT delorie DOT com
MIME-Version: 1.0

i have written on object I/O

the code : -

----------------------------------------------------------------------------------------------
#include <fstream>
#include <iostream>

using namespace std;

class student
{
	private:
	int iReg_no;
	char cName[20];
	public:
	void setRegno()
	{
		cout <<"\nEnter the Registration number: ";
		cin >>iReg_no;
	}
	void setName()
	{
		cout <<"\nEnter the name of the student: ";
		cin >>cName;
	}
	int getRegno()
	{
		return iReg_no;
	}
	char *getName()
	{
		return cName;
	}
};

int main()
{
	ofstream Sfil("studfile.dat");
	char ch;
	student svar;
	cout <<"Want to enter more records\n";
	cin >>ch;
	while (1)
	{
		if (ch == 'y')
		{
		
			if (ch =='n')
				break;
			svar.setRegno();
			svar.setName();
			Sfil.write((char*)&svar, sizeof(student));
			cout <<"Want to enter more records: ";
			cin >>ch;
		}
		if (ch == 'n')
			break;
	}
	Sfil.close();
	cout <<"\nDo you want to view the contents of the
file (y/n)? ";
	cin >>ch;
	if (ch =='y')
	{
		ifstream Sfil("studfile.dat");
		Sfil.read((char *)&svar, sizeof(student));
		while(Sfil)
		{
			cout <<"\n Registration Number is: "
<<svar.getRegno();
			cout <<"\n Student Name is " <<svar.getName();
			Sfil.read((char*)&svar, sizeof(student));
		}
	}
	return 0;
}
-------------------------------------------------------------------------------------------------------------------

on running the program the first time: -

--------------------------------------------------------------------------------------------------

Want to enter more records
y

Enter the Registration number: 5655

Enter the name of the student: pradyut
Want to enter more records: y

Enter the Registration number: 43453

Enter the name of the student: praddy
Want to enter more records: y

Enter the Registration number: 9090

Enter the name of the student: jam
Want to enter more records: y

Enter the Registration number: 55

Enter the name of the student: bear
Want to enter more records: y

Enter the Registration number: 3303

Enter the name of the student: gam
Want to enter more records: y

Enter the Registration number: 3234

Enter the name of the student: gain
Want to enter more records: n

Do you want to view the contents of the file (y/n)? y

 Registration Number is: 5655
 Student Name is pradyut
 Registration Number is: 1919942656
 Student Name is addy
 Registration Number is: 7168362
 Student Name is dy
 Registration Number is: 2030072417
 Student Name is
 Registration Number is: 30976
 Student Name is
--------------------------------------------------------------------------------------

the second time : -

-------------------------------------------------------------------------------------

Want to enter more records
y

Enter the Registration number: 444

Enter the name of the student: yes
Want to enter more records: y

Enter the Registration number: 88

Enter the name of the student: jam
Want to enter more records: n

Do you want to view the contents of the file (y/n)? y

 Registration Number is: 444
 Student Name is yes

---------------------------------------------------------------------------------------

any help 
thanks 

pradyut
http://pradyut.tk

India


		
_______________________________
Do you Yahoo!?
Win 1 of 4,000 free domain names from Yahoo! Enter now.
http://promotions.yahoo.com/goldrush

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019