Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Input file stream to stdin

Status
Not open for further replies.

Skatanic

Programmer
Jun 25, 2002
37
US
Is there a way to set an input file stream:
ifstream in;
so that it will read not from a file, but from stdin instead? What I am trying to do is make it so the input can come from either stdin or from a file. If there is no file specified it will be from stdin. If there is a file, it will read from the file, then at the end switch to reading from stdin. I would like to make it so that I can use ifstream and not FILE*. I can do it using the file pointers, but I already set up the WHOLE program using streams and I don't want to switch them to fscanf.

Thanks,
Skatanic
 
Another one of those times where you figure it out before you get a reply. So in case anyone was wondering here is what I did...

istream *input;
ifstream in;

// ...

if (!in) {
input = &cin;
} else {
input = new ifstream(in);
}

Then whenever in is empty, just delete input and do
input = &cin;
and it will go back to stdin;

In order to input using that istream, do this:
*input >> blah;

That's it...
-Skatanic
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top