0

Im trying to use a pointer to point to my desired input stream depending on user decision. This is what ive got so far.

string fileName = "test.txt";
ifsteam = myFile;
myFile.open(fileName.c_str(), ifstream::in);
istream * myStream;    
if (file_mode) {
    myStream = &myFile;
} else {
    myStream = &cin;
}
string out;
while (myStream >> out) {
    cout << out << endl;
}

The problem seems to be that nothing is streaming from myStream to out.

Any help would be greatly appreciated.

Tristus
  • 123
  • 1
  • 13

1 Answers1

1

One problem is that 'myStream' is a pointer to istream, but in the while loop it is being used as an instance of istream. You need to deference the pointer for the code to work correctly. For instance:

while (*myStream >> out) {
    cout << out << endl;
}
Jorge Torres
  • 1,426
  • 12
  • 21