In file handling, I came across ifstream, ofstream and fstream. Can anyone tell me the main difference between these?
-
[`ifstream`](https://en.cppreference.com/w/cpp/io/basic_ifstream), [`ofstream`](https://en.cppreference.com/w/cpp/io/basic_ofstream), [`fstream`](https://en.cppreference.com/w/cpp/io/basic_fstream). – Enlico May 21 '21 at 05:14
-
Dup: https://stackoverflow.com/questions/49563597/using-ifstream-ofstream-and-fstream – Enlico May 21 '21 at 05:17
3 Answers
This is how the class hierarchy looks like:
From https://www.cplusplus.com/img/iostream.gif
The three classes that deal with file handling are:
basic_ifstream
basic_ofstream
basic_fstream
ifstream
, ofstream
and fstream
are "char
" template specializations which means they are nothing but basic_ifstream<char>
, basic_ofstream<char>
and basic_fstream<char>
i.e. they deal with reading and writing char
s from a file.
ifstream
is input file stream which allows you to read the contents of a file.ofstream
is output file stream which allows you to write contents to a file.fstream
allows both reading from and writing to files by default. However, you can have anfstream
behave like anifstream
orofstream
by passing in theios::open_mode
flag.
ios::openmode
Flags
The open mode flags are:
Flag | Description |
---|---|
ios::app |
All write operations must occur at the end of the file |
ios::binary |
Open in binary mode |
ios::in |
Open for reading |
ios::out |
Open for writing |
ios::trunc |
Empty the contents of the file after opening |
ios::ate |
Go to the end of the file after opening |
These flags are additive which means you can combine multiple flags using the bitwise OR |
operator. If I want to open the file in binary mode and append, I can combine the flags as follows:
ios::binary | ios::app
ifstream
always has theios::in
flag set and it cannot be removed. Similary,ofstream
always has theios::out
flag set and it cannot be removed. Any other flags added will be combined withios::in
forifstream
andios::out
forofstream
- On the other hand, if you do not pass any flags to
fstream
, the default isios::in | ios::out
, so you can read from as well as write to the file. But if you specify a flag explicitly forfstream
likeios::in
, it will be opened only for reading, like anifstream
.
How to pass flags?
You can do so in the constructor or when calling open()
:
ifstream infile("filepath", ios::binary); //Open the file for reading in binary mode, ios::in will always be set
ofstream outfile("filepath", ios::trunc); // Open the file for writing and clear its contents, ios::out is implicitly set
fstream inoutfile("filepath") // default flag will be: ios::in | ios::out hence both reads and writes possible
fstream infile("filepath", ios::in) // file will be opened in read mode like fstream
It is basically possible to never use ifstream
and ofstream
and always use fstream
with the required flags. But it is prone to accidental errors while setting the flags. Hence, using ifstream
you can be sure that writes will never occur and with ofstream
only writes will take place.

- 4,359
- 2
- 22
- 44
-
1The last sentence should be amended to something like: Hence, using ifstream you can be sure that reads will always be enabled independent from using (or not using) any masks. By analogy, writes will always be enabled for ofstream. – Artur Opalinski Jan 16 '22 at 21:11
ifstream
is for input only.
ofstream
is for output only.
fstream
can be used for both/either input and/or output.

- 4,359
- 2
- 22
- 44

- 555,201
- 31
- 458
- 770
Key is in the names:
ifstream
= "input file stream" which is a type ofistream
or "input stream"ofstream
= "output file stream" which is a type ofostream
or "output stream"fstream
= "(bidirectional) file stream" as iniostream
("input/output stream") as in includes both aspects through inheritance

- 208,517
- 23
- 234
- 262