0% found this document useful (0 votes)
8 views9 pages

OOPs Assignment 5 Mayuri

The document contains a C++ assignment submitted by Mayuri Agrawal, which includes three programming tasks: reading a text file, creating a simple account management class with file handling, and explaining file pointer functions. It details the implementation of file reading and writing, account deposit and withdrawal methods, and the use of seekg(), seekp(), tellg(), and tellp() functions for random access in binary files. Additionally, it discusses manipulators in C++ and their differences from ios functions.

Uploaded by

Ashutosh Kewat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views9 pages

OOPs Assignment 5 Mayuri

The document contains a C++ assignment submitted by Mayuri Agrawal, which includes three programming tasks: reading a text file, creating a simple account management class with file handling, and explaining file pointer functions. It details the implementation of file reading and writing, account deposit and withdrawal methods, and the use of seekg(), seekp(), tellg(), and tellp() functions for random access in binary files. Additionally, it discusses manipulators in C++ and their differences from ios functions.

Uploaded by

Ashutosh Kewat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

[Department of Information Technology]

(230203)
[Assignment 5]
Submitted to: - Proff. Sanjeev Sharma sir

Submitted by: - Mayuri Agrawal


(0901AI201033)
Q.1 Write a C++ Program for reading a file to read a
text file

Text file named Mayuri.txt

#include <fstream>
#include <iostream>
#include <string>

using namespace std;

int main() {
fstream my_file;
my_file.open("Mayuri.txt");
if (!my_file) {
cout << "No such file";
} else {
string line;

while (1) {
getline(my_file, line);

if (!my_file)
break;

cout << line << endl;


}
}
my_file.close();
return 0;
}

Output

Q.2. Write a C++ program to create a simple class named


Account and write methods to deposit, withdraw
amount from the account and display balance Using file
handling.
Make a Text file named Account.txt
#include <fstream>
#include <iostream>
#include <string>

using namespace std;

class Account {
public:
Account() {
statement_file.open("Account.txt");

statement_file.seekg(0, ios::end);
if (statement_file.tellg() == 0) {
statement_file.seekg(0, ios::beg);

balance = 1200;
Name = "Mayuri";
account_number = 20;

write();
} else {
get_info();
}
}

~Account() { statement_file.close(); }

void write() {
statement_file.close();
statement_file.open("Account.txt", std::fstream::out | std::fstr
eam::trunc);
statement_file.seekg(0, ios::beg);
statement_file << account_number << "\n" << Name << "\n" << bala
nce;
}

void get_info() {
string line;
statement_file.seekg(0, ios::beg);

getline(statement_file, line);
account_number = stoi(line);

getline(statement_file, line);
Name = line;
getline(statement_file, line);
balance = stod(line);
}

void display() {
std::cout << "\nName : " << Name << "\n"
<< "Account Number : " << account_number
<< "\nBalance : " << balance << std::endl;
}

void deposit(double ammount) {


balance += ammount;
write();
}

void withdraw(double ammount) {


balance -= ammount;
write();
}

private:
int account_number;
string Name;
double balance;
fstream statement_file;
};

int main() {
Account One;

int choice;
double data;

cout << "\nAccount Manipulator"


<< "\n 1.) Show Details"
<< "\n 2.) Deposit"
<< "\n 3.) Withdraw"
<< "\n 4.) Exit";
while (1) {

cout << "\nEnter Your Choice: ";


cin >> choice;

switch (choice) {
case 1:
One.display();
break;
case 2:
cout << "\nEnter Ammount to deposit : ";
cin >> data;
One.deposit(data);
break;
case 3:
cout << "\nEnter Ammount to withdraw : ";
cin >> data;
One.withdraw(data);
break;
default:
return 0;
}
}
}
Output

Q.3 Explain the role of seekg(), seekp(), tellg(),


tellp(), function in the process of random access
in a binary file.
Seekg() - seekg() is used to move the get pointer to a
desired location with respect to a
reference point.
Syntax: file_pointer.seekg (number of bytes ,Reference point);
Example: fin.seekg(10,ios:;beg);

seekp() - is used to move the put pointer to a desired


location with respect to a reference point.
Syntax; file_pointer.seekp(number of bytes „Reference point);
Example: fout,seekp(10,ios:;beg);

tellg() - is used to know where the get pointer is in


a file,
Syntax: file_pointer.tellg();
Example: int posn = fin.tellg();

tellp() - is used to know where the put pointer is in


a file,
Syntax: file_pointer.tellp(;
Example: int posn=fout,tellp();

Q4, what is manipulator? Difference between


manipulators and ios Function?
Manipulators are functions specifically designed to be used in
conjunction with the insertion («) and extraction (>>)
operators on stream objects, for example:
cout « boolalpha;
They are still regular functions and can also be called as
any other function using a stream object as argument, for
example:
boolalpha (cout);:
Manipulators are used to change formatting parameters on
streams and to insert or extract certain special characters.

Difference between Manipulators and ios Function –


1. ios functions returns value while manipulators does not.

2.we can not create own ios functions while we can create our own
manipulators.

3.ios functions are single and not possible to be combined while


manipulators are possible to be applied in chain.

4.ios function needs while manipulators needs

5.ios functions are member functions while manipulators are non-


member functions.

You might also like