How would one go about casting a ifstream into a istream. I figure since ifstream is a child of istream I should be able to do so but I have been having problems with such a task.
std::istream & inputStr = std::cin;
std::ostream & outputStr = std::cout;
if(argc == 3){
std::fstream inputFile;
inputFile.open(argv[1], std::fstream::in);
if(!inputFile){
std::cerr << "Error opening input file";
exit(1);
}
inputStr = inputFile;
.....
}
Note: Both cin and all the ifstream objects can be passed as a reference to istream . As you know, ifstream almost works the same as istream .
istream Class − The istream class handles the input stream in c++ programming language. These input stream objects are used to read and interpret the input as a sequence of characters. The cin handles the input. ostream class − The ostream class handles the output stream in c++ programming language.
Bookmark this question.
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.
No cast is necessary.
#include <fstream>
int main()
{
using namespace std;
ifstream f;
istream& s = f;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With