+ 2
Can iostream be replaced with fstream?
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.
+ 2
As I know iostream is used for input-output operation. fstream is used for files manipulation. You can use both of them.
+ 2
Damyan Petkov but in case making a project we use only fstream instead of iostream..
+ 2
Timon Paßlick yes I did...
+ 2
Timon Paßlick I am asking that can fstream be used instead of iostream...no using the turbo and online compiler...
+ 2
Timon Paßlick means its legal to use
+ 1
//compiles
#include <fstream>
#include <iostream>
int main()
{
std::fstream fs;
std::iostream& ios = fs;
}
+ 1
Shuvam Lal So you never use the console?
+ 1
Shuvam Lal But the console works with istreams and ostreams.
Or did you use the old C functions?
+ 1
fstream extends iostream.
+ 1
What do you mean with legal?