Directing a file into standard input

Hi,

I am doing an exercise from Thinking In C++ and it suggests "redirecting a file into the program's standard input".

Could someone please suggest how I can accomplish that.
Something like this should work:
1
2
3
4
5
6
7
8
9
streambuf *buf = cin.rdbuf(); // keep the cin buffer to restore it

ifstream file ( "something"); // create the file

cin.rdbuf( file.rdbuf() ); // redirect input

// use cin - it will read from the file -

cin.rdbuf ( buf ); // reset cin buffer to the previous one 

What is the point of that anyway when you can just manipulate the ifstream directly as though it were an iostream? The interface and operators are largely the same.
I'm not sure if this is what you mean, but in a *nix OS, on the command line you can pipe or redirect another programs output into your own program. For example, "cat file | ./yourprogram".

All your program has to do is read from cin to access the input.
Last edited on
Topic archived. No new replies allowed.