0% found this document useful (0 votes)
18 views21 pages

OOP L1 L001 Exp9

The document outlines an experiment on Console I/O Operations in C++ as part of a course at SVKM’s NMIMS University. It details the aims, learning outcomes, and various methods for performing formatted and unformatted console I/O operations, as well as file handling in C++. Additionally, it includes tasks for students to implement code examples and submit their work in a specified format.

Uploaded by

srishtik.685
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)
18 views21 pages

OOP L1 L001 Exp9

The document outlines an experiment on Console I/O Operations in C++ as part of a course at SVKM’s NMIMS University. It details the aims, learning outcomes, and various methods for performing formatted and unformatted console I/O operations, as well as file handling in C++. Additionally, it includes tasks for students to implement code examples and submit their work in a specified format.

Uploaded by

srishtik.685
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/ 21

SVKM’s NMIMS University

Mukesh Patel School of Technology Management & Engineering


COURSE: Object Oriented Programming and Design
Experiment: 9
PART A

(PART A: TO BE REFFERED BY STUDENTS)

A.1 Aim: To study and implement Console I/O Operations in C++

A.2 Learning Outcomes: Learner would be able to

1. Describe the fundamental concepts of Console I/O Operations in C++.


2. Implement the Formatted, Unformatted I/O Operation functions.
3. Describe usage of File in C++.
4. Create File in C++

A.3 Theory:

In C++ Programming, the console IO operations are performed using the header file iostream.h.
This header file provides two objects cin and cout to perform input and output operations
respectively. There are mainly two types of consol IO operations.

1. Unformatted consol IO -

2. Formatted consol IO

A.3.1 Unformatted Console I/O:-


We use the following built-in functions to perform operations of type unformatted consol IO
operations.
⇢ get( ) and put( )
The get( ) is a method of cin object used to input a single character from the standard input device
(keyboard). Its main property is that it allows wide spaces and newline character. The get()
function does not have any return value (void get( )).
The put() is a method of cout object and it is used to print the specified character on standard
output device (monitor).
#include <iostream>
using namespace std;
int main()

{
char ch;
cout<<"Press any key: ";

ch = cin.get();
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Object Oriented Programming and Design
cout << "You have pressed: ";
cout.put(ch); return 0;

}
⇢ getline(char *buffer,int size) and write(char * buffer, int n)
The getline(char *buffer,int size) is a method of cin object and it is used to input a string with
multiple spaces.
The write(char * buffer, int n) is a method of cout object and it is used to read n character from
buffer variable.
#include <iostream>

using namespace std;

int main()
{
char ch[20];

cout<<"What is your favourite website: ";


cin.getline(ch, 20);

cout <<endl<< "visit: www.";


cout.write(ch, 17);
cout<<".com"<<endl;

return 0;
}
⇢ cin and cout objects
The cin is the object used to take input value of any variable of any type. It must be used with an
overloaded operator “>>”.
The cout is an object used to print string and variable values. It must be used with an overloaded
operator “<<”.
A.3.2. Formatted Console I/O :

The C++ programming language provides the following built-in functions to display the output in
formatted form. These built-in functions are available in the header file iomanip.h.
⇢ setw(int) and setfill(char)
The setw( int ) is a method used to set width of the output.
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Object Oriented Programming and Design
The setfill(char) is a method used to fill specified character at unused space.
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
int x=10;

cout<<setw(10);
cout<<x<<endl;

cout<<setw(10)<<setfill('*')<<x<<endl;

return 0;
}

A.3.3. I/O Streams:


C++ comes with libraries that provide us with many ways for performing input and output. In C++ input and output are performed in the form of
a sequence of bytes or more commonly known as streams.

Input Stream: If the direction of flow of bytes is from the device(for example, Keyboard) to the main memory then this process is called input.

Output Stream: If the direction of flow of bytes is opposite, i.e. from main memory to device( display screen ) then this process is called output.

Standard output stream (cout): Usually the standard output device is the display screen. The C++ cout statement is the instance of the ostream
class. It is used to produce output on the standard output device which is usually the display screen.

The data needed to be displayed on the screen is inserted in the standard output stream (cout) using the insertion operator( <<).

standard input stream (cin): Usually the input device in a computer is the keyboard. C++ cin statement is the instance of the class istream and
is used to read input from the standard input device which is usually a keyboard.

The extraction operator(>>) is used along with the object cin for reading inputs.

The extraction operator extracts the data from the object cin which is entered using the keyboard.

Unbuffered Error Streams:


#include <iostream>
using namespace std;
int main()
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Object Oriented Programming and Design
{
cerr << "An error occurred";
return 0;
}
Buffered Error Streams:
#include <iostream>
using namespace std;
int main()
{
clog << "An error occurred";
return 0;
}
A.3.4: File Streams in C++

Reasons to use Files:


1. Convenient way to deal large quantities of data.
2. Store data permanently (until file is deleted).
3. Avoid typing data into program multiple times.
4. Share data between programs

We need to know:

• How to "connect" file to program?


• How to tell the program to read data?
• How to tell the program to write data?
• Error checking and handling end of file (eof)?

Example: Reading (input) and writing (output) data to a file.


SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Object Oriented Programming and Design

A.4 TASK(s) TO BE COMPLETED:


Task 1: Implement all the programs given in the theory sections A.3.

Task2: Create your own file named as “MyFile_YourRollNo” and write following details in
your file: for 10 people in a table use gdb compiler, open in excel too

Name:

Roll number

College name

Append the following details in the same file later on:

Address

Email address
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Object Oriented Programming and Design

PART B

(PART B: TO BE COMPLETED BY STUDENTS)

Students must submit the soft copy as per following segments. The soft copy must be uploaded on
the portal at the end of the practical. The filename should be OOP_batch_rollno_experiment no,
Example: OOP_E1_E001_Exp8

Roll No.: L001 Name: Srishti

Prog/Yr/Sem: B.Tech CSEDS/Year Batch: L1


I/Sem II

Date of Experiment: 18-03-2025 Date of Submission: 19-03-2025

B.1 Code snapshot:

Task 1: -

1.
#include <iostream>
using namespace std;
int main()
{
char ch;
cout<<"Press any key: ";
ch = cin.get();
cout << "You have pressed: ";
cout.put(ch); return 0;
}

2.
#include <iostream>
using namespace std;
int main()
{
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Object Oriented Programming and Design
char ch[20];
cout<<"What is your favourite website: ";
cin.getline(ch, 20);
cout <<endl<< "visit: www.";
cout.write(ch, 11);
cout<<".com"<<endl;
return 0;
}

3.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int x=10;
cout<<setw(10);
cout<<x<<endl;
cout<<setw(10)<<setfill('*')<<x<<endl;
return 0;
}

4.
#include <iostream>
using namespace std;
int main()
{
cerr << "An error occurred";
return 0;
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Object Oriented Programming and Design
}

5.
#include <iostream>
using namespace std;
int main()
{
clog << "An error occurred";
return 0;
}

6.
#include<fstream>
#include<iostream>
using namespace std;
int main()
{
char name[50];
ofstream ofile;
ofile.open("abc.txt");
cout<<"Writing to the file"<<endl;
cout<<"Enter your name:"<<endl;
cin.getline(name,50);
if(ofile.fail())
{
cout<<"Output file could not be opened.\n";
exit(1);
}
else
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Object Oriented Programming and Design
ofile<<name<<endl;
ofile.close();
ifstream ifile;
ifile.open("abc.txt");
cout<<"Reading from the file"<<endl;
if(ifile.fail())
{
cout<<"Input file could not be opened.\n";
exit(1);
}
else
ifile>>name;
cout<<name<<endl;
ifile.close();
return 0;
}
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Object Oriented Programming and Design

#include<fstream>

#include<iostream>

using namespace std;

int main()

char name[50];

ofstream ofile;

ofile.open("abc.csv");

cout<<"Writing to the file"<<endl;


SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Object Oriented Programming and Design
cout<<"Enter your name:"<<endl;

cin.getline(name,50);

if(ofile.fail())

cout<<"Output file could not be opened.\n";

exit(1);

else

ofile<<name<<endl;

ofile.close();

ifstream ifile;

ifile.open("abc.csv");

cout<<"Reading from the file"<<endl;

if(ifile.fail())

cout<<"Input file could not be opened.\n";

exit(1);

else

ifile>>name;

cout<<name<<endl;

ifile.close();

return 0;

}
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Object Oriented Programming and Design

Task 2: -

#include <fstream>

#include <iostream>

using namespace std;

int main()

ofstream ofile;

ofile.open("MyFile_YourRollNo.csv", ios::app);

if (ofile.fail())

cout << "Output file could not be opened.\n";


SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Object Oriented Programming and Design
exit(1);

for (int i = 0; i < 10; i++)

char name[50], college[100], address[100], email[100];

int rollno;

cout << "Writing to the file"<< endl;

cout << "Enter your roll number: " << endl;

cin >> rollno;

cin.get();

cout << "Enter your name: " << endl;

cin.getline(name, 50);

cout << "Enter your college: " << endl;

cin.getline(college, 100);

cout << "Enter your address: " << endl;

cin.getline(address, 100);

cout << "Enter your email address: " << endl;

cin.getline(email, 100);

ofile << "\"" << name << "\"," << rollno << ",\"" << college << "\",\"" << address << "\",\""
<< email << "\"" << endl;

cout << "Data written successfully!" << endl << endl;

ofile.close();

ifstream ifile("MyFile_YourRollNo.csv");

if (ifile.fail()) {

cout << "Input file could not be opened.\n";


SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Object Oriented Programming and Design
exit(1);

cout << "Reading from the file:\n";

char name[50], college[100], address[100], email[100];

int rollno;

while (ifile.getline(name, 50, ','))

ifile >> rollno;

ifile.get();

ifile.getline(college, 100, ',');

ifile.getline(address, 100, ',');

ifile.getline(email, 100);

cout << name << " " << rollno << " " << college << " " << address << " " << email << endl;

ifile.close();

return 0;

}
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Object Oriented Programming and Design
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Object Oriented Programming and Design
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Object Oriented Programming and Design

B.3 Observations and Learnings:

1. Task 1 was insightful. It really put everything into perspective about Input/Output streams. It is
not something that comes easily to me. I did need a little time to grasp what to do and how
exactly is the code being implemented
2. Task 2 was a good combination of the concepts we studied about. It took time to implement the
code in csv. I used append mode to put the different parameters into different fields.

B.4 Question of Curiosity:

Explore the world wide web and answer the following questions:

1. Write a code in C++ to read and write data into file


#include <iostream>
#include <fstream>
using namespace std;
int main() {
ofstream ofile("data.txt");
if (ofile.fail())
{
cout << "Error opening file for writing!" << endl;
return 1;
}
string data;
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Object Oriented Programming and Design
cout << "Enter data to write into the file: ";
getline(cin, data);
ofile << data << endl;
ofile.close();
ifstream ifile("data.txt");
if (ifile.fail())
{
cout << "Error opening file for reading!" << endl;
return 1;
}
cout << "Reading from file: ";
while (getline(ifile, data)) {
cout << data << endl;
}
ifile.close();
return 0;
}

2. Write a code in C++ to append data in the file.


#include <iostream>
#include <fstream>
using namespace std;
int main() {

ofstream ofile("data.txt", ios::app);


if (ofile.fail()) {
cout << "Error opening file for appending!" << endl;
return 1;
}
string data;
cout << "Enter data to append to the file: ";
getline(cin, data);
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Object Oriented Programming and Design
ofile << data << endl;
ofile.close();
cout << "Data appended successfully!" << endl;
return 0;
}

3. Reading and Writing an Array of objects to file


#include <iostream>
#include <fstream>
using namespace std;
class student {
public:
char name[50];
int rollno;
void getdata()
{
cout << "Enter roll number: ";
cin >> rollno;
cin.get();
cout << "Enter name: ";
cin.getline(name, 50);
}
void display()
{
cout << "Name: " << name << ", Roll No: " << rollno << endl;
}
};
int main()
{
student st[3];
ofstream ofile("students.dat", ios::binary);
if (ofile.fail())
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Object Oriented Programming and Design
{
cout << "Error opening file for writing!" << endl;
return 1;
}
cout << "Enter details of 3 students:" << endl;
for (int i = 0; i < 3; i++)
{
st[i].getdata();
ofile.write((char*)&st[i], sizeof(student));
}
ofile.close();
ifstream ifile("students.dat", ios::binary);
if (ifile.fail())
{
cout << "Error opening file for reading!" << endl;
return 1;
}
cout << "\nReading student data from file:\n";
student s;
while (ifile.read((char*)&s, sizeof(student)))
{ // Read from file
s.display();
}
ifile.close();
return 0;
}

B.5 Conclusion (Learning Outcomes): You need to write PERSONALISED CONCLUSION


in your own words based on following learning outcomes

1. Describe the fundamental concepts of Console I/O Operations in C++.


2. Implement the Formatted, Unformatted I/O Operation functions.
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Object Oriented Programming and Design
3. Describe usage of File in C++.
4. Create File in C++

You might also like