On 15 Srp, 06:31, masood.iq...@lycos.com wrote:
In the stdio library, supported by C, we can set a file pointer to
stdin or stdout ot stderr (e.g. FILE* fp = stdin;). Can we do
something similar with ifstream and ofstream in C++? i.e. can we set
the ifstream to stdin and the ofstream to stdout or stderr? If the
answer is yes, please provide a code snippet.
Thanks,
Masood
Do all your stuff in some function or method:
#include <iostream>
#include <fstream>
void DoSomethingWithStream(std::ostream& os)
{
os << "Do something here\n";
}
int main()
{
// You can call DoSomethingWithStream with ostream as well as with
ofstream
std::ofstream ifs("filename", std::ios::in);
DoSomethingWithStream(ifs);
ifs.close();
DoSomethingWithStream(std::cout);
}