Archive

Posts Tagged ‘Cpp File Handling’

C++ Program to implement File Handling With Class Objects – Q32

February 1, 2010 Leave a comment

Q32. Program to implement File Handling With Class Objects:

Define a class to represent a bank account. Include the following members:
  i) Depositor Name
 ii) Account Number
iii) Balance Amount

Member Function
  i) To Assign Initial values(opening balance Rs. 1000 by default)
 ii) To deposit an amount
iii) To withdraw an amount(if possible)
iv) To display the current balance

Write a file based program to store the records of at least ten accounts in “ACCOUNT_DETAIL” data file and perform the required manipulations- DEPOSIT, WITHDRAW & BAL_ENQUIRY using the file on a given account. The changes after each deposit and withdrawal should be updated in the given file.

… from College notes (BCA/MCA assignments):

#include <fstream.h>
#include <stdlib.h>
#include <iomanip.h>
#include <conio.h>

const int MAX = 100;

enum bool{_false, _true};

class Cbank{
	private:
		int accNo;
		char name[20];
		float bal;
	public:
		Cbank() : accNo(100){}
		void getAcc(int );
		bool accPresent(int );
		void saveBal(int, float );
		void dispAcc() const;
		void dispAll() const;
	};

void Cbank :: getAcc(int i){
	accNo += i;
	cout<<"\n Enter Name: ";
	cin>>name;
	cout<<"\n Enter Amount for opening Acc.: ";
	cin>>bal;
	}



bool Cbank :: accPresent(int _acnt){
	if(accNo == _acnt)
		return _true;
	else
		return _false;
	}

void Cbank :: saveBal(int type, float _amt){
	if(type == 2){		// Deposit.
		bal += _amt;
		cout<<"\n Balance Updated.";
		}
	else if(type == 3){	// Withdrawl.
		if(bal >= _amt){
			bal -= _amt;
			cout<<"\n Balance Updated.";
			}
		else
			cout<<"\n Not Enough Funds on Account, SORRY !!";
		}
	}

void Cbank :: dispAcc() const{
	cout<<"\n Account Information:-";
	cout<<"\n ~~~~~~~~~~~~~~~~~~~";
	cout<<"\n Account No.: "<<accNo;
	cout<<"\n Holders Name: "<<name;
	cout<<"\n Balance: "<<bal;
	}

void Cbank :: dispAll() const{
	cout<<endl<<setw(8)<<accNo<<setw(20)<<name<<setw(20)<<bal;
	}

void main(){
	int ch, i, _tot = 0;
	int _acc;
	float _amt;
	char nx;
	bool found;
	Cbank *Obank = new(Cbank[MAX]);
	fstream fstr;
	fstr.open("Bank.txt", ios::in | ios::nocreate);
	if(fstr){
		while(fstr){
			fstr.read((char *) &Obank[_tot], 
sizeof(Obank[_tot]));
			_tot++;
			}
		_tot -= 2;
		}
	fstr.close();
	// Data Retrieved form Disk.
	while(1){
		clrscr();
		cout<<"\n BANK AUTOMATION";
		cout<<"\n ~~~~~~~~~~~~~~~";
		cout<<"\n 1 -> Create Account.";
		cout<<"\n 2 -> Deposit Money.";
		cout<<"\n 3 -> Withdrawl Money.";
		cout<<"\n 4 -> Balance Inqury.";
		cout<<"\n 5 -> Display Accounts.";
		cout<<"\n 6 -> Exit.";
		cout<<"\n\n Enter your choice: ";
		cin>>ch;
		clrscr();
		switch(ch){
			case 1:	// Create Acc.
				while(_tot < MAX){
					cout<<"\n Account No.: "<<_tot+1;
					Obank[_tot].getAcc(_tot);
					cout<<"\n Want to add more (y/n): ";
					cin>>nx;
					_tot++;
					if(nx == 'n')
						break;
					}
				break;
			case 2:	// Deposit Money.
				found = _false;
				cout<<"\n Enter Account No: ";
				cin>>_acc;
				for(i=0; i<=_tot; i++){
					if(Obank[i].accPresent(_acc)){
						found = _true;
						break;
						}
					}
				if(!found){
					cout<<"\n Account Not Found.";
					break;
					}
				else{
					cout<<"\n Enter Amount to Deposit: ";
					cin>>_amt;
					Obank[i].saveBal(ch, _amt);
					}
				break;
			case 3:	// Withdrawl Money.
				found = _false;
				cout<<"\n Enter Account No: ";
				cin>>_acc;
				for(i=0; i<=_tot; i++){
					if(Obank[i].accPresent(_acc)){
						found = _true;
						break;
						}
					}
				if(!found){
					cout<<"\n Account Not Found.";
					break;
					}
				else{
					cout<<"\n Enter Amount to Withdrawl:";
					cin>>_amt;
					Obank[i].saveBal(ch, _amt);
					}
				break;
			case 4:	// Balance Inquiry.
				found = _false;
				cout<<"\n Enter Account No: ";
				cin>>_acc;
				for(i=0; i<=_tot; i++){
					if(Obank[i].accPresent(_acc)){
						found = _true;
						break;
						}
					}
				if(!found){
					cout<<"\n Account Not Found.";
					break;
					}
				else{
					Obank[i].dispAcc();
					}
				break;
			case 5:	// Display Acc.
				cout<<"\n Account Information:-";
				cout<<"\n ~~~~~~~~~~~~~~~~~~~";
				cout<<"\n ACC No."<<setw(20)
    <<"NAME"<<setw(20)<<"BALANCE";
				cout<<"\n``````````````````````````````````
  ``````````````````````````````````";
				for(i=0; i<_tot; i++)
					Obank[i].dispAll();
				break;
			case 6:	// Exit;
				fstr.open("Bank.txt", ios::out);
				for(i=0; i<=_tot; i++)
					fstr.write((char *) &Obank[i], 
sizeof(Obank[i]));
				fstr.close();
				// Data Saved to Disk.
				delete []Obank;
				exit(1);
			default:
				cout<<"\n Enter Appropriate Choice.";
			} // end of switch.
		getch();
		} // end of while.
	} // end of main.

 

Output:

BANK AUTOMATION

1 -> Create Account.
2 -> Deposit Money.
3 -> Withdrawl Money.
4 -> Balance Inqury.
5 -> Display Accounts.
6 -> Exit.

Enter your choice:1

Account No.: 1
Enter Name: Manoj

Enter Amount for opening Acc.: 1000

Want to add more (y/n): y

Account No.: 2
Enter Name: Nitin

Enter Amount for opening Acc.: 2000

Want to add more (y/n): n

Account Information:-

ACC No.                NAME             BALANCE
```````````````````````````````````````````````````````````````````
     100                 Manoj               1000
     101                 Nitin                 2000

Advertisement

C++ Program to implement File Handling with Command Line Arguments – Q31

January 31, 2010 Leave a comment

Q31. Program to implement File Handling with Command Line Arguments:

Copy the contents of an existing file to a new file the names of which are taken as command line arguments. The program should handle errors if the given first file name does not already exist.

… from College notes (BCA/MCA assignments):

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

void main(int argc, char *argv[]){
	char src[12], dest[12];
	char buff[1000];
	int len;
	clrscr();

	if(argc > 3){
		cout<<"\n Illegal Number of Arguments."<<endl;
		exit(1);
		}
	else if(argc == 2){
		cout<<"\n Enter the Destination File."<<endl;
		cin>>dest;
		}
	else if(argc == 1){
		cout<<"\n Enter Source and Destination File."<<endl;
		cin>>src;
		cin>>dest;
		}
	else{
		strcpy(src, argv[1]);
		strcpy(dest, argv[2]);
		}

	ifstream fin;
	fin.open(src, ios::in);

	if(!fin){
		cerr<<"\n File Does not Exist.";
		getch();
		exit(1);
		}
	fin.read((char*)&buff, sizeof(buff));
	fin.close();

	len = strlen(buff) - 1;

	ofstream fout;
	fout.open(dest, ios::out);
	fout.write((char*)&buff, len);
	fout.close();

	cout<<"\n File copied Successfully.";

	getch();
	}

 

Output:

C:\TC\MANOJ>31_FH.exe man.txt abc.txt

File Copied Successfully.


C++ Program to implement File Handling by using ifstream & ofstream (Prg-2) – Q30

January 30, 2010 Leave a comment

Q30. Program to implement Function Overloading:

Write a paragraph (Atleast 2 lines of sentences) in a file. Read the same file in three different ways –
  i) Reverse the whole paragraph.
 ii) Line by Line Reverse the whole para.
iii) Break the pragraph int two equal halves. Reverse the second half of the pargaraph.

… from College notes (BCA/MCA assignments):

#include <string.h>
#include <fstream.h>
#include <conio.h>

void main(){
	char para[500], ln[80];
	int len, i, half;
	clrscr();

	cout<<"\n Enter a Paragraph (Quit by ^Z):-";
	cout<<"\n ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
	cin.getline(para, 500, '^Z');
	len = strlen(para);

	ofstream fout;
	fout.open("para.txt", ios::out);
	fout.write((char *) &para, len);
	fout.close();

	ifstream fin;
	cout<<"\n Reverse the Whole Paragraph:-";
	cout<<"\n ~~~~~~~~~~~~~~~~~~~~~~~~~~~";
	fin.open("para.txt", ios::in);
	fin.read((char *) &para, sizeof(para));
	len = strlen(para);
	for(i=len; i>=0; i--)
		cout<<para[i];
	fin.close();
	getch();

	cout<<"\n Line by line Reverse the Whole Paragraph:-";
	cout<<"\n ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
	fin.open("para.txt", ios::in);
	while(fin){
		fin.getline(ln, 80);
		len = strlen(ln);
		for(i=len; i>=0; i--)
			cout<<ln[i];
		cout<<endl;
		}
	fin.close();
	getch();

	cout<<"\n Breaking The Para Into 2 halves and Reversing the 2nd Para:-";
	cout<<"\n ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
	fin.open("para.txt", ios::in);
	fin.read((char *) &para, sizeof(para));
	len = strlen(para);
	half = len / 2;
	for(i=0; i<=half; i++)
		cout<<para[i];
	for(i=len; i>half; i--)
		cout<<para[i];
	fin.close();
	getch();
	}

 

Output:

Enter a Paragraph (Quit by ^Z):-

My name is Manoj Pandey.
I study in APEEJAY Institute.
I’m Doing MCA from there.
I live in Dwarka Sec. 6.
^Z

Reverse the Whole Paragraph:-

.6 .ceS akrawD ni evil I
.ereht morf ACM gnioD m’I
.etutitsnI YAJEEPA ni yduts I
.yednaP jonaM si eman yM

Line by line Reverse the Whole Paragraph:-

.yednaP jonaM si eman yM
.etutitsnI YAJEEPA ni yduts I
.ereht morf ACM gnioD m’I
.6 .ceS akrawD ni evil I

Breaking The Para Into 2 halves and Reversing the 2nd Para:-

My name is Manoj Pandey.
I study in APEEJAY Institute.
.6 .ceS akrawD ni evil I
.ereht morf ACM gnioD m’I


C++ Program to implement File Handling by using ifstream & ofstream (Prg-1) – Q29

January 29, 2010 Leave a comment

Q29. Program to implement File Handling by using ifstream & ofstream:

Get the Rollno, Names and Marks Obtained of N students. The data should be stored in “RESULT” data file.
Use the same file to print the result along with aggregate percentage for all the students.

… from College notes (BCA/MCA assignments):

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

class Cstud{
	private:
		char name[10];
		int roll, marks;
	public:
		void getdata();
		void putdata();
		int calc() const;
	};

void Cstud :: getdata(){
	cout<<"\n Enter Name: ";
	cin>>name;
	cout<<"\n Enter Roll No: ";
	cin>>roll;
	cout<<"\n Enter Marks: ";
	cin>>marks;
	}

int Cstud :: calc() const{
	return marks;
	}

void Cstud :: putdata(){
	cout<<"\n Name: "<<name;
	cout<<"\n Roll No: "<<roll;
	cout<<"\n Marks: "<<marks;
	}

void main(){
	int n, tot, avg;
	Cstud Ostu1, Ostu2;
	clrscr();
	cout<<"\n How many Students: ";
	cin>>n;
	ofstream fout;
	fout.open("result.txt", ios::out);
	for(int i=0; i<n; i++){
		cout<<"\n Student No: "<<i+1;
		Ostu1.getdata();
		fout.write((char *) &Ostu1, sizeof(Ostu1));
		}
	fout.close();

	ifstream fin;
	fin.open("result.txt", ios::in);
	tot = 0;
	clrscr();
	cout<<"\n Data read from File:- \n";
	for(i=0; i<n; i++){
		fin.read((char *) &Ostu2, sizeof(Ostu2));
		cout<<"\n\n Student No: "<<i+1;
		Ostu2.putdata();
		tot += Ostu2.calc();
		}
	avg = tot/n;
	cout<<"\n\n Total marks: "<<tot;
	cout<<"\n\n Average Marks: "<<avg;
	fin.close();
	getch();
	}

 

Output:

How many Students: 5

Student No: 1
Enter Name: manoj
Enter Roll No: 204
Enter Marks: 200

Student No: 2
Enter Name: Bhanu
Enter Roll No: 104
Enter Marks: 300

Student No: 3
Enter Name: Nitin
Enter Roll No: 214
Enter Marks: 350

Student No: 4
Enter Name: Prakash
Enter Roll No: 304
Enter Marks: 250

Student No: 5
Enter Name: Shilpi
Enter Roll No: 123
Enter Marks: 400

Student No: 1
Name: Manoj Roll No: 204 Marks: 200

Student No: 2
Name: Bhanu Roll No: 104 Marks: 300

Student No: 3
Name: Nitin Roll No: 214 Marks: 350

Student No: 4
Name: Prakash Roll No: 304 Marks: 250

Student No: 5
Name: Shilpi Roll No: 123 Marks: 400

Total marks: 1500
Average Marks: 300