How to assign streams

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?
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
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
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.
Thank you firedraco . I will try your suggestion.
Topic archived. No new replies allowed.