Can iostream be replaced with fstream? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Can iostream be replaced with fstream?

7th Apr 2018, 8:51 AM
Shuvam Lal
Shuvam Lal - avatar
11 Answers
+ 4
<iostream> includes <istream> and <ostream>. <fstream> includes <ifstream> and <ofstream>. As for why an object of iostream can reference an object of fstream, this is because <ifstream> and <ofstream> are derived from <istream> and <ostream> respective, as has been pointed out by Timon Paßlick: fstream extends iostream. Now, <iostream> cannot replace <fstream> because a base class does not contain all the members of the derived class. Similarly, <fstream> cannot replace <iostream> because some members of the base class cannot be invoked by an object of the derived class.
7th Apr 2018, 12:15 PM
Hatsy Rei
Hatsy Rei - avatar
+ 2
As I know iostream is used for input-output operation. fstream is used for files manipulation. You can use both of them.
7th Apr 2018, 9:23 AM
Damyan Petkov
Damyan Petkov - avatar
+ 2
Damyan Petkov but in case making a project we use only fstream instead of iostream..
7th Apr 2018, 11:12 AM
Shuvam Lal
Shuvam Lal - avatar
+ 2
Timon Paßlick yes I did...
7th Apr 2018, 11:16 AM
Shuvam Lal
Shuvam Lal - avatar
+ 2
Timon Paßlick I am asking that can fstream be used instead of iostream...no using the turbo and online compiler...
7th Apr 2018, 11:20 AM
Shuvam Lal
Shuvam Lal - avatar
+ 2
Timon Paßlick means its legal to use
7th Apr 2018, 11:28 AM
Shuvam Lal
Shuvam Lal - avatar
+ 1
//compiles #include <fstream> #include <iostream> int main() { std::fstream fs; std::iostream& ios = fs; }
7th Apr 2018, 10:57 AM
Timon Paßlick
+ 1
Shuvam Lal So you never use the console?
7th Apr 2018, 11:13 AM
Timon Paßlick
+ 1
Shuvam Lal But the console works with istreams and ostreams. Or did you use the old C functions?
7th Apr 2018, 11:17 AM
Timon Paßlick
+ 1
fstream extends iostream.
7th Apr 2018, 11:27 AM
Timon Paßlick
+ 1
What do you mean with legal?
7th Apr 2018, 11:49 AM
Timon Paßlick