Getting the stream of the file to which output is redirected.
How can we know the name of the stream to which output is being redirected through a C++ program?
For example, I have a program 1.cpp whose output is being redirected to a file a.txt
i.e., a.out > a.txt
Now, I want to know the name of the file to which output is being redirected in the program itself.
How can I do that?
No. Redirection, piping and general screen printing are facilities of the OS, not your program. Additionally, there are other tools, such as tee which would complicate the matter if you could. Suppose you had ./a.out | tee a.txt would you want that tee was receiving your output or that it was ultimately sent to a.txt?