SlideShare a Scribd company logo
Managing I/O Console
using C++
C++ Stream Classes
• Stream and stream classes : to implement I/O
operations with the console and disk files.
• It is a sequence of bytes(or interface)..
• It acts as a source from which input data can
be obtained.
• or as destination to which the output can be
sent.
• Source stream is called Input stream.
• Destination stream is called Output stream.
• Data in the Input stream can come from
Keyboard etc.
• Similarly, data in the output stream can go to
the screen.
• C++ provides standard iostream library to
operate with streams.
• Cin and cout are the predefined streams .
• If bytes flow from a device like a keyboard, a
disk drive, or a network connection etc. to
main memory, this is called input operation .
• and if bytes flow from main memory to a
device like a display screen, a printer, a disk
drive, or a network connection, etc., this is
called output operation.
• istream is a general purpose input stream. cin
is an example of an istream.
• ostream is a general purpose output stream.
cout is the example of ostream.
• ifstream is an input file stream. It is a special
kind of an istream that reads in data from a
data file.
• ofstream is an output file stream. It is a
special kind of ostream that writes data out to
a data file.
• C++ provides the following classes to perform
output and input of characters to/from files:
ofstream: Stream class to write on files
• ifstream: Stream class to read from files
• fstream: Stream class to both read and write
from/to files.
Console I/O operations
There are mainly two types of console I/O
operations form:
• Unformatted console input output
• Formatted console input output
Unformatted input output operations
The following are operations of unformatted
input / output operations:
A) void get()
• It is a method of cin object used to input a
single character from keyboard. But its main
property is that it allows wide spaces and
newline character.
• Syntax:
char c=cin.get();
Example
#include<iostream>
using namespace std;
int main()
{
char c=cin.get();
cout<<c<<endl;
return 0;
}
void put()
• It is a method of cout object and it is used to
print the specified character on the screen or
monitor.
• Syntax:
cout.put(variable / character);
Example
#include<iostream>
using namespace std;
int main()
{
char c=cin.get();
cout.put(c); //Here it prints the value of variable c;
cout.put('c'); //Here it prints the character 'c';
return 0;
}
getline(char ,int size)
• This is a method of cin object and it is used to
input a string with multiple spaces.
• Syntax:
char x[30];
cin.getline(x,30);
Example
#include<iostream>
using namespace std;
int main()
{
cout<<"Enter name :";
char c[10];
cin.getline(c,10); //It takes 10 charcters as
input
cout<<c<<endl;
return 0;
}
write(char , int n)
• It is a method of cout object. This method is
used to write n character from buffer variable.
• Syntax:
cout.write(x,2);
Example
#include<iostream>
using namespace std;
int main()
{
cout<<"Enter name : ";
char c[10];
cin.getline(c,10); //It takes 10 charcters as input;
cout.write(c,9); //It reads only 9 character from buffer c;
return 0;
}
cin
• It is the method to take input any variable /
character / string.
• Syntax:
cin>>variable / character / String / ;
Example
#include<iostream>
using namespace std;
int main()
{
int num;
char ch;
string str;
cout<<"Enter Number"<<endl;
cin>>num; //Inputs a variable;
cout<<"Enter Character"<<endl;
cin>>ch; //Inputs a character;
cout<<"Enter String"<<endl;
cin>>str; //Inputs a string;
return 0;
}
cout
• This method is used to print variable / string /
character.
Syntax:
cout<< variable / charcter / string;
Example
#include<iostream>
using namespace std;
int main()
{
int num=100;
char ch='X';
string str="Deepak";
cout<<"Number is "<<num<<endl; //Prints value of variable;
cout<<"Character is "<<ch<<endl; //Prints character;
cout<<"String is "<<str<<endl; //Prints string;
return 0;
}
Formatted console input output operations
• In formatted console input output operations we
uses functions to make output in perfect
alignment.
• C++ provides many function to convert any file
into perfect aligned format.
• These functions are available in header
file <iomanip>.
• iomanip refers input output manipulations.
width(n)
• This function is used to set width of the
output.
• Syntax:
cout<<setw(int n);
Example
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int x=10;
cout<<setw(20)<<x;
return 0;
}
fill(char)
• This function is used to fill specified character
at unused space.
• Syntax:
cout<<setfill('character')<<variable;
Example
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int x=10;
cout<<setw(20);
cout<<setfill('#')<<x;
return 0;
}
precison(n)
• This method is used for setting floating point
of the output.
• Syntax:
cout<<setprecision('int n')<<variable;
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
float x=10.12345;
cout<<setprecision(5)<<x;
return 0;
}
Working with Files
• The data are stored in the secondary device
using the concept of files.
• Files are the collection of related data stored
in a particular area on the disk.
• Programs can be written to perform read and
write operations on these files.
Working with files generally requires the
following kinds of data communication
methodologies:
• Data transfer between console units.
• Data transfer between the program and the
disk file.
• We use the header file <fstream>
File Handling Classes
• ofstream: It represents output Stream and this is
used for writing in files.
• ifstream: It represents input Stream and this is
used for reading from files.
• fstream: It represents both output Stream and
input Stream. So it can read from files and write
to files.
Operations in File Handling
• Creating a file: open()
• Reading data: read()
• Writing new data: write()
• Closing a file: close()
Opening & Closing a file in C++
Opening a file
Files can be opened in two ways. They are:
• Using constructor function of the class.
- Useful when we use only one file.
• Using member function open of the class.
- Manage multiple files using one stream.
Using constructor function of the class
The syntax of opening a file in C++ is:
open (filename, mode);
There are some mode flags used for file opening. These are:
• ios::app: append mode
• ios::ate: open a file in this mode for read/write controlling to
the end of the file
• ios::in: open file in this mode for reading
• ios::out: open file in this mode for writing
• ios::trunk: when any file already exists, its contents will be
truncated before file opening
• When any C++ program terminates, it
automatically flushes out all the streams,
releases all the allocated memory and closes
all the opened files.
• But it is good to use the close() function to
close the file related streams and it is a
member of ifsream, ofstream and fstream
objects.
• The structure of using this function is:
void close();
Reading from and writing to file
• While doing C++ program, programmers write
information to a file from the program using the
stream insertion operator (<<) and reads
information using the stream extraction
operator (>>).
• The only difference is that for files programmers
need to use an ofstream or fstream object
instead of the cout object and ifstream or
fstream object instead of the cin object.
Opening file using constructor
• This involves the following steps:-
i) Create a file stream object to manage the
stream using the appropriate class.
ii) Initialize the file object with desired
filename.
ofstream outfile(“result”);
outfile<<<“total”;
ifstream infile(“data”);
infile>>number;
Working with Single file
#include<fstream.h>
void main()
{
ofstream outf(“item”);
cout<<“enter name”;
char name[30];
cin>>name;
outf<<name;
float cost;
cin>>cost;
outf<<cost;
outf.close();
ifstream inf(“item”);
inf>>name;
inf>>cost;
cout<<name;
cout<<cost;
inf.close();
}
• When a file is opened for writing only, a new
file is created if there is no file of that name.
• If file by that name exists already, then its
contents are deleted and file is presented as a
clean file.
• Without losing the original contents, open an
existing file for updating.
Opening file using open() function
• Function open() can be used open multiple files
that uses the same stream object.
fstream stream_obj;
stream_obj.open(“file_name”);
• First file is closed before opening the second one.
This is necessary because stream can be
connected to only one file at a time.
Example
ofstream outfile;
outfile.open(“data1”);
--------------
---------------
outfile.close();
outfile.open(“data2”);
--------------
---------------
outfile.close();
Create file with Open() function
#include<fstream.h>
void main()
{
ofstream fout;
fout.open(“counting”);
fout<<“US”;
fout<<“UK”;
fout<<“india”;
fout.close();
fout.open(“capital”);
fout<<“washington”;
fout<<“london”;
fout<<“delhi”;
fout.close();
• //reading the files(PTO)
int N=80;
char line[N];
ifstream fin;
fin.open(“country”);
while(fin)
{
fin.getline(line,N);
cout<<line;
}
fin.close();
fin.open(“capital”);
while(fin)
{
fin.getline(line,N);
cout<<line;
}
fin.close();
}
Detecting end-of-file
• While(fin) //ifstream object
• Another method:
• if(fin.eof()!=0);
• Eof() is a member of ios class.
• This statement terminates the program on
reaching the end of file.
More about open file modes
• object.open(“filename”); ///one argument
• open() can take 2 arguments.
object.open(“filename”,mode);
• Member function contain default values for
second argument and therefore they use
default values in the absence of actual values.
• Ios::in
• Ios::out

More Related Content

PPTX
Stream classes in C++
Shyam Gupta
 
PPTX
FILE OPERATIONS.pptx
DeepasCSE
 
PPT
new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...
AzanMehdi
 
PPT
File handling in_c
sanya6900
 
PPT
file_handling_in_c.ppt......................................
nadoj47203
 
PPT
file_handling_in_c.pptbbbbbbbbbbbbbbbbbbbbb
SanskritiGupta39
 
PPTX
CPP17 - File IO
Michael Heron
 
PDF
streams and files
Mariam Butt
 
Stream classes in C++
Shyam Gupta
 
FILE OPERATIONS.pptx
DeepasCSE
 
new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...
AzanMehdi
 
File handling in_c
sanya6900
 
file_handling_in_c.ppt......................................
nadoj47203
 
file_handling_in_c.pptbbbbbbbbbbbbbbbbbbbbb
SanskritiGupta39
 
CPP17 - File IO
Michael Heron
 
streams and files
Mariam Butt
 

Similar to 13 file handling in C++.pptx oops object oriented programming (20)

DOCX
File handling in c++
Daniel Nyagechi
 
PDF
Files in C++.pdf is the notes of cpp for reference
anuvayalil5525
 
PPTX
Working with files in c++. file handling
tfluid16
 
PDF
Files and streams
Pranali Chaudhari
 
PPTX
file handling final3333.pptx
radhushri
 
PPTX
basics of file handling
pinkpreet_kaur
 
PPTX
Basics of file handling
pinkpreet_kaur
 
PDF
Data file handling
Prof. Dr. K. Adisesha
 
PPTX
Files in c++
NivethaJeyaraman
 
PPTX
File management in C++
apoorvaverma33
 
PPTX
Streams and Files
Munazza-Mah-Jabeen
 
PPTX
Introduction-to-Streams-and-Files-in-C.pptx
mubarrahnaeem12
 
PDF
C++ prgms io file unit 7
Ananda Kumar HN
 
PDF
C++ Files and Streams
Ahmed Farag
 
PPTX
Filesin c++
HalaiHansaika
 
PPT
Input and output in C++
Nilesh Dalvi
 
PPTX
file.pptx 43dcsddsafgdewdvvbghghsdwweffr
abdelhamidatef1
 
PPTX
Data file handling
TAlha MAlik
 
PPTX
Object Oriented Programming Using C++: Ch12 Streams and Files.pptx
RashidFaridChishti
 
PPTX
Object Oriented Programming using C++: Ch12 Streams and Files.pptx
RashidFaridChishti
 
File handling in c++
Daniel Nyagechi
 
Files in C++.pdf is the notes of cpp for reference
anuvayalil5525
 
Working with files in c++. file handling
tfluid16
 
Files and streams
Pranali Chaudhari
 
file handling final3333.pptx
radhushri
 
basics of file handling
pinkpreet_kaur
 
Basics of file handling
pinkpreet_kaur
 
Data file handling
Prof. Dr. K. Adisesha
 
Files in c++
NivethaJeyaraman
 
File management in C++
apoorvaverma33
 
Streams and Files
Munazza-Mah-Jabeen
 
Introduction-to-Streams-and-Files-in-C.pptx
mubarrahnaeem12
 
C++ prgms io file unit 7
Ananda Kumar HN
 
C++ Files and Streams
Ahmed Farag
 
Filesin c++
HalaiHansaika
 
Input and output in C++
Nilesh Dalvi
 
file.pptx 43dcsddsafgdewdvvbghghsdwweffr
abdelhamidatef1
 
Data file handling
TAlha MAlik
 
Object Oriented Programming Using C++: Ch12 Streams and Files.pptx
RashidFaridChishti
 
Object Oriented Programming using C++: Ch12 Streams and Files.pptx
RashidFaridChishti
 
Ad

Recently uploaded (20)

PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
PDF
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
PDF
Event Presentation Google Cloud Next Extended 2025
minhtrietgect
 
PDF
Advances in Ultra High Voltage (UHV) Transmission and Distribution Systems.pdf
Nabajyoti Banik
 
PPTX
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
PDF
Software Development Methodologies in 2025
KodekX
 
PPTX
ChatGPT's Deck on The Enduring Legacy of Fax Machines
Greg Swan
 
PDF
Best ERP System for Manufacturing in India | Elite Mindz
Elite Mindz
 
PDF
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
 
PDF
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
PDF
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
PDF
DevOps & Developer Experience Summer BBQ
AUGNYC
 
PPTX
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
PPTX
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
PDF
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
PDF
This slide provides an overview Technology
mineshkharadi333
 
PDF
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
Event Presentation Google Cloud Next Extended 2025
minhtrietgect
 
Advances in Ultra High Voltage (UHV) Transmission and Distribution Systems.pdf
Nabajyoti Banik
 
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
Software Development Methodologies in 2025
KodekX
 
ChatGPT's Deck on The Enduring Legacy of Fax Machines
Greg Swan
 
Best ERP System for Manufacturing in India | Elite Mindz
Elite Mindz
 
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
 
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
DevOps & Developer Experience Summer BBQ
AUGNYC
 
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
This slide provides an overview Technology
mineshkharadi333
 
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
Ad

13 file handling in C++.pptx oops object oriented programming

  • 2. C++ Stream Classes • Stream and stream classes : to implement I/O operations with the console and disk files. • It is a sequence of bytes(or interface).. • It acts as a source from which input data can be obtained. • or as destination to which the output can be sent. • Source stream is called Input stream. • Destination stream is called Output stream.
  • 3. • Data in the Input stream can come from Keyboard etc. • Similarly, data in the output stream can go to the screen. • C++ provides standard iostream library to operate with streams. • Cin and cout are the predefined streams .
  • 4. • If bytes flow from a device like a keyboard, a disk drive, or a network connection etc. to main memory, this is called input operation . • and if bytes flow from main memory to a device like a display screen, a printer, a disk drive, or a network connection, etc., this is called output operation.
  • 5. • istream is a general purpose input stream. cin is an example of an istream. • ostream is a general purpose output stream. cout is the example of ostream. • ifstream is an input file stream. It is a special kind of an istream that reads in data from a data file. • ofstream is an output file stream. It is a special kind of ostream that writes data out to a data file.
  • 6. • C++ provides the following classes to perform output and input of characters to/from files: ofstream: Stream class to write on files • ifstream: Stream class to read from files • fstream: Stream class to both read and write from/to files.
  • 7. Console I/O operations There are mainly two types of console I/O operations form: • Unformatted console input output • Formatted console input output
  • 8. Unformatted input output operations The following are operations of unformatted input / output operations: A) void get() • It is a method of cin object used to input a single character from keyboard. But its main property is that it allows wide spaces and newline character. • Syntax: char c=cin.get();
  • 9. Example #include<iostream> using namespace std; int main() { char c=cin.get(); cout<<c<<endl; return 0; }
  • 10. void put() • It is a method of cout object and it is used to print the specified character on the screen or monitor. • Syntax: cout.put(variable / character);
  • 11. Example #include<iostream> using namespace std; int main() { char c=cin.get(); cout.put(c); //Here it prints the value of variable c; cout.put('c'); //Here it prints the character 'c'; return 0; }
  • 12. getline(char ,int size) • This is a method of cin object and it is used to input a string with multiple spaces. • Syntax: char x[30]; cin.getline(x,30);
  • 13. Example #include<iostream> using namespace std; int main() { cout<<"Enter name :"; char c[10]; cin.getline(c,10); //It takes 10 charcters as input cout<<c<<endl; return 0; }
  • 14. write(char , int n) • It is a method of cout object. This method is used to write n character from buffer variable. • Syntax: cout.write(x,2);
  • 15. Example #include<iostream> using namespace std; int main() { cout<<"Enter name : "; char c[10]; cin.getline(c,10); //It takes 10 charcters as input; cout.write(c,9); //It reads only 9 character from buffer c; return 0; }
  • 16. cin • It is the method to take input any variable / character / string. • Syntax: cin>>variable / character / String / ;
  • 17. Example #include<iostream> using namespace std; int main() { int num; char ch; string str; cout<<"Enter Number"<<endl; cin>>num; //Inputs a variable; cout<<"Enter Character"<<endl; cin>>ch; //Inputs a character; cout<<"Enter String"<<endl; cin>>str; //Inputs a string; return 0; }
  • 18. cout • This method is used to print variable / string / character. Syntax: cout<< variable / charcter / string;
  • 19. Example #include<iostream> using namespace std; int main() { int num=100; char ch='X'; string str="Deepak"; cout<<"Number is "<<num<<endl; //Prints value of variable; cout<<"Character is "<<ch<<endl; //Prints character; cout<<"String is "<<str<<endl; //Prints string; return 0; }
  • 20. Formatted console input output operations
  • 21. • In formatted console input output operations we uses functions to make output in perfect alignment. • C++ provides many function to convert any file into perfect aligned format. • These functions are available in header file <iomanip>. • iomanip refers input output manipulations.
  • 22. width(n) • This function is used to set width of the output. • Syntax: cout<<setw(int n);
  • 23. Example #include<iostream> #include<iomanip> using namespace std; int main() { int x=10; cout<<setw(20)<<x; return 0; }
  • 24. fill(char) • This function is used to fill specified character at unused space. • Syntax: cout<<setfill('character')<<variable;
  • 25. Example #include<iostream> #include<iomanip> using namespace std; int main() { int x=10; cout<<setw(20); cout<<setfill('#')<<x; return 0; }
  • 26. precison(n) • This method is used for setting floating point of the output. • Syntax: cout<<setprecision('int n')<<variable;
  • 27. #include<iostream> #include<iomanip> using namespace std; int main() { float x=10.12345; cout<<setprecision(5)<<x; return 0; }
  • 28. Working with Files • The data are stored in the secondary device using the concept of files. • Files are the collection of related data stored in a particular area on the disk. • Programs can be written to perform read and write operations on these files.
  • 29. Working with files generally requires the following kinds of data communication methodologies: • Data transfer between console units. • Data transfer between the program and the disk file. • We use the header file <fstream>
  • 30. File Handling Classes • ofstream: It represents output Stream and this is used for writing in files. • ifstream: It represents input Stream and this is used for reading from files. • fstream: It represents both output Stream and input Stream. So it can read from files and write to files.
  • 31. Operations in File Handling • Creating a file: open() • Reading data: read() • Writing new data: write() • Closing a file: close()
  • 32. Opening & Closing a file in C++
  • 33. Opening a file Files can be opened in two ways. They are: • Using constructor function of the class. - Useful when we use only one file. • Using member function open of the class. - Manage multiple files using one stream.
  • 34. Using constructor function of the class The syntax of opening a file in C++ is: open (filename, mode); There are some mode flags used for file opening. These are: • ios::app: append mode • ios::ate: open a file in this mode for read/write controlling to the end of the file • ios::in: open file in this mode for reading • ios::out: open file in this mode for writing • ios::trunk: when any file already exists, its contents will be truncated before file opening
  • 35. • When any C++ program terminates, it automatically flushes out all the streams, releases all the allocated memory and closes all the opened files. • But it is good to use the close() function to close the file related streams and it is a member of ifsream, ofstream and fstream objects. • The structure of using this function is: void close();
  • 36. Reading from and writing to file • While doing C++ program, programmers write information to a file from the program using the stream insertion operator (<<) and reads information using the stream extraction operator (>>). • The only difference is that for files programmers need to use an ofstream or fstream object instead of the cout object and ifstream or fstream object instead of the cin object.
  • 37. Opening file using constructor • This involves the following steps:- i) Create a file stream object to manage the stream using the appropriate class. ii) Initialize the file object with desired filename. ofstream outfile(“result”); outfile<<<“total”; ifstream infile(“data”); infile>>number;
  • 38. Working with Single file #include<fstream.h> void main() { ofstream outf(“item”); cout<<“enter name”; char name[30]; cin>>name; outf<<name; float cost; cin>>cost; outf<<cost; outf.close(); ifstream inf(“item”); inf>>name; inf>>cost; cout<<name; cout<<cost; inf.close(); }
  • 39. • When a file is opened for writing only, a new file is created if there is no file of that name. • If file by that name exists already, then its contents are deleted and file is presented as a clean file. • Without losing the original contents, open an existing file for updating.
  • 40. Opening file using open() function • Function open() can be used open multiple files that uses the same stream object. fstream stream_obj; stream_obj.open(“file_name”); • First file is closed before opening the second one. This is necessary because stream can be connected to only one file at a time.
  • 42. Create file with Open() function #include<fstream.h> void main() { ofstream fout; fout.open(“counting”); fout<<“US”; fout<<“UK”; fout<<“india”; fout.close(); fout.open(“capital”); fout<<“washington”; fout<<“london”; fout<<“delhi”; fout.close(); • //reading the files(PTO)
  • 43. int N=80; char line[N]; ifstream fin; fin.open(“country”); while(fin) { fin.getline(line,N); cout<<line; } fin.close(); fin.open(“capital”); while(fin) { fin.getline(line,N); cout<<line; } fin.close(); }
  • 44. Detecting end-of-file • While(fin) //ifstream object • Another method: • if(fin.eof()!=0); • Eof() is a member of ios class. • This statement terminates the program on reaching the end of file.
  • 45. More about open file modes • object.open(“filename”); ///one argument • open() can take 2 arguments. object.open(“filename”,mode); • Member function contain default values for second argument and therefore they use default values in the absence of actual values. • Ios::in • Ios::out