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++
PPTX
FILE OPERATIONS.pptx
PPT
new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...
PPT
File handling in_c
PPT
file_handling_in_c.pptbbbbbbbbbbbbbbbbbbbbb
PPT
file_handling_in_c.ppt......................................
PPTX
CPP17 - File IO
PDF
streams and files
Stream classes in C++
FILE OPERATIONS.pptx
new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...
File handling in_c
file_handling_in_c.pptbbbbbbbbbbbbbbbbbbbbb
file_handling_in_c.ppt......................................
CPP17 - File IO
streams and files

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

DOCX
File handling in c++
PDF
Files in C++.pdf is the notes of cpp for reference
PPTX
Working with files in c++. file handling
PDF
Files and streams
PPTX
file handling final3333.pptx
PPTX
basics of file handling
PPTX
Basics of file handling
PDF
Data file handling
PPTX
Files in c++
PPTX
File management in C++
PPTX
Streams and Files
PPTX
Introduction-to-Streams-and-Files-in-C.pptx
PDF
C++ prgms io file unit 7
PDF
C++ Files and Streams
PPTX
Filesin c++
PPT
Input and output in C++
PPTX
file.pptx 43dcsddsafgdewdvvbghghsdwweffr
PPTX
Data file handling
PPTX
Object Oriented Programming Using C++: Ch12 Streams and Files.pptx
PPTX
Object Oriented Programming using C++: Ch12 Streams and Files.pptx
File handling in c++
Files in C++.pdf is the notes of cpp for reference
Working with files in c++. file handling
Files and streams
file handling final3333.pptx
basics of file handling
Basics of file handling
Data file handling
Files in c++
File management in C++
Streams and Files
Introduction-to-Streams-and-Files-in-C.pptx
C++ prgms io file unit 7
C++ Files and Streams
Filesin c++
Input and output in C++
file.pptx 43dcsddsafgdewdvvbghghsdwweffr
Data file handling
Object Oriented Programming Using C++: Ch12 Streams and Files.pptx
Object Oriented Programming using C++: Ch12 Streams and Files.pptx
Ad

Recently uploaded (20)

PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
KodekX | Application Modernization Development
PDF
NewMind AI Monthly Chronicles - July 2025
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
A Presentation on Artificial Intelligence
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Modernizing your data center with Dell and AMD
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Machine learning based COVID-19 study performance prediction
PDF
cuic standard and advanced reporting.pdf
PPTX
Big Data Technologies - Introduction.pptx
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
KodekX | Application Modernization Development
NewMind AI Monthly Chronicles - July 2025
20250228 LYD VKU AI Blended-Learning.pptx
The Rise and Fall of 3GPP – Time for a Sabbatical?
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Reach Out and Touch Someone: Haptics and Empathic Computing
“AI and Expert System Decision Support & Business Intelligence Systems”
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Per capita expenditure prediction using model stacking based on satellite ima...
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
A Presentation on Artificial Intelligence
Dropbox Q2 2025 Financial Results & Investor Presentation
Building Integrated photovoltaic BIPV_UPV.pdf
Modernizing your data center with Dell and AMD
Mobile App Security Testing_ A Comprehensive Guide.pdf
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Machine learning based COVID-19 study performance prediction
cuic standard and advanced reporting.pdf
Big Data Technologies - Introduction.pptx
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