Apr 9, 2009 at 3:25am UTC
I'm trying to assign references to an open stream, i.e.
istream x, &y;
x.open();
y = &x; // or
y = &cin; // or something
(Or something that works).
Anyway to do this?
Apr 9, 2009 at 3:34am UTC
references must be initialed to something.
1 2 3 4 5 6 7 8 9 10
#include <iostream>
using namespace std;
int main() {
char test[256];
istream& i = cin;
ostream& o = cout;
i >> test;
o << test;
return 0;
}
Last edited on Apr 9, 2009 at 3:38am UTC
Apr 9, 2009 at 4:31am UTC
Thank you. I understand what you've done but I have a somewhat different issue. At the time the program starts I don't know what I want. That is
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <ios>
#include <iostream>
istream& ii;
int main() {
char test[256];
istream* i;
if ( <something> == true ) {
i = &cin;
} else {
i.open("name" , ios::in);
}
*i >> test;
ii = i;
}
Will this work?
Last edited on Apr 9, 2009 at 4:33am UTC
Apr 9, 2009 at 5:18am UTC
Maybe...I think line 11 will have to be i->open("name" , ios::in);
(since you are using a pointer instead of just the object). Also, make sure your char array test is initialized to something.
Apr 9, 2009 at 5:42am UTC
Thank you firedraco . I will try your suggestion.