我想做类似这样的事情,但仍然遇到错误,关于istream的复制赋值运算符受保护。我想有一种方法可以将输入从cin切换到我的程序中某个未知点的文件输入。将ifstream复制到istream C++ 14
#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;
}
}
流不分配/可复制。你究竟用'in = f;'来完成什么?这没有意义。只需在'f'上直接使用'operator >>'。 –