I want to do something like this but keep running into an error, regarding the copy assignment operator for istream is protected. I want to have a way to switch input from cin to input from a file at some unknown point in my program.
#include <iostream>
#include <fstream>
using namespace std;
int main() {
istream &in = cin;
ifstream f{"file.txt"};
in = f;
// Then I want to read input from the file.
string s;
while(in >> s) {
cout << s << endl;
}
}