0% found this document useful (0 votes)
119 views4 pages

Classes For File Stream Operations

The document describes the classes in C++ for file stream operations, including ifstream, ofstream, and fstream, which are essential for data storage and retrieval. It outlines the hierarchy of these classes and their functions, such as input/output handling and file modes for opening files. Additionally, it provides examples of how to read and write files using these classes in C++.

Uploaded by

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

Classes For File Stream Operations

The document describes the classes in C++ for file stream operations, including ifstream, ofstream, and fstream, which are essential for data storage and retrieval. It outlines the hierarchy of these classes and their functions, such as input/output handling and file modes for opening files. Additionally, it provides examples of how to read and write files using these classes in C++.

Uploaded by

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

Classes for File stream operations :-

The I/O system of C++ contains a set of classes which define the file
handling methods. These include ifstream, ofstream and fstream classes.
These classes are derived from fstream and from the corresponding
iostream class. These classes, designed to manage the disk files, are
declared in fstream and therefore we must include this file in any program
that uses files. File handling is essential for data storage and retrieval in
applications.
1. ios:-
 ios stands for input output stream.
 This class is the base class for other classes in this class hierarchy.
 This class contains the necessary facilities that are used by all the other
derived classes for input and output operations.

2. istream:-
 istream stands for input stream.
 This class is derived from the class ‘ios’.
 This class handle input stream.
 The extraction operator(>>) is overloaded in this class to handle input
streams from files to the program execution.
 This class declares input functions such as get(), getline() and read().

3. ostream:-
 ostream stands for output stream.
 This class is derived from the class ‘ios’.
 This class handle output stream.
 The insertion operator(<<) is overloaded in this class to handle output
streams to files from the program execution.
 This class declares output functions such as put() and write().
4. streambuf:-
 This class contains a pointer which points to the buffer which is used to
manage the input and output streams.
5. fstreambase:-
 This class provides operations common to the file streams. Serves as a
base for fstream, ifstream and ofstream class.
 This class contains open() and close() function.
6. ifstream:-
 This class provides input operations.
 It contains open() function with default input mode.
 Inherits the functions get(), getline(), read(), seekg() and tellg() functions
from the istream.
7. ofstream:-
 This class provides output operations.
 It contains open() function with default output mode.
 Inherits the functions put(), write(), seekp() and tellp() functions from the
ostream.
8. fstream:-
 This class provides support for simultaneous input and output operations.
 Inherits all the functions from istream and ostream classes through
iostream.
9. filebuf:-
 Its purpose is to set the file buffers to read and write.
 We can also use file buffer member function to determine the length of
the file.

In C++, files are mainly dealt by using three classes fstream, ifstream,
ofstream available in fstream headerfile.
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.
Now the first step to open the particular file for read or write operation. We
can open file by
1. passing file name in constructor at the time of object creation
2. using the open method

For e.g.
Open File by using constructor
ifstream (const char* filename, ios_base::openmode mode = ios_base::in);
ifstream fin(filename, openmode) by default openmode = ios::in
ifstream fin(“filename”);
Open File by using open method
Calling of default constructor
ifstream fin;
fin.open(filename, openmode)
fin.open(“filename”);

Modes :
Member Stands
Constant For Access

File open for reading: the internal stream buffer


ios::in input
supports input operations.

File open for writing: the internal stream buffer


ios::out output
supports output operations.

Operations are performed in binary mode rather


ios::binary binary
than text.

ios::ate at end The output position starts at the end of the file.

All output operations happen at the end of the


ios::app append
file, appending to its existing contents.

Any contents that existed in the file before it is


ios::trunc truncate
open are discarded.

ios::nocreate Do not Does not allow to create new file if it does not
Member Stands
Constant For Access

create exist.

Do not
ios::noreplace Does not replace old file with new file.
replace

Both ios::app and ios::ate take us to the end of the file when it is opened.
The difference between the two modes is that ios :: app allow us to add data
to the end of the file only, while ios :: ate mode permits us add data or to
modify the existing data anywhere in the file.
Default Open Modes :
ifstream ios::in

ofstream ios::out

fstream ios::in | ios::out

Problem Statement : To read and write a File in C++.


Examples:
Input :
Welcome in GeeksforGeeks. Best way to learn things.
-1
Output :
Welcome in GeeksforGeeks. Best way to learn things.
Below is the implementation by using ifstream & ofstream classes.

You might also like