Read from cin or a file
When I try to compile the code istream in; if (argc==1) in=cin; else { ifstream ifn(argv[1]); in=ifn; } gcc fails, complaining that operator= is private. Is there any way to set an istream to different values based on a condition? You can replace cin's streambuf with another, and in some programs this is simpler than the general strategy of passing around istreams without referring to cin directly. int main(int argc, char* argv[]) { ifstream input; streambuf* orig_cin = 0; if (argc >= 2) { input.open(argv[1]); if (!input) return 1; orig_cin = cin.rdbuf(input.rdbuf()); cin.tie(0); // tied to