I want to redirect cout to a file when needed. Here is the main part of my test program, but I can't figure out some input was not directed to the file.
ucla();
ucla() {
std::cout << "inside ucla" << std::endl;
}
int main() {
const char* outName = argv[2];
std::string outFile(outName);
std::streambuf *coutbuf, *termbuf;
termbuf = std::cout.rdbuf();
if (!outFile.empty()) {
std::ofstream outstr;
outstr.open(argv[2]);
coutbuf = outstr.rdbuf();
std::cout.rdbuf(coutbuf);
std::cout << "here" << std::endl;
} else {std::cout.rdbuf(termbuf);}
std::cout << "this file " << std::endl;
ucla();
}
When the program is run, only here was written to the file, I expect this file inside ucla I searched earlier threads, and I can't figure out what is missing. Thanks