mercredi 1 octobre 2014

Can I create my own output stream that is different from stdout and stderr?


Vote count:

0





The following symbolic constants are defined for file streams:
STDIN_FILENO File number of stdin. It is 0.
STDOUT_FILENO File number of stdout. It is 1.
STDERR_FILENO File number of stderr. It is 2.


grepping all of my header files, I find this to be true.



[/usr]grep -r "STDIN_FILENO" include
include/unistd.h:#define STDIN_FILENO 0 /* Standard input. */

[/usr] grep -r "STDOUT_FILENO" include
include/unistd.h:#define STDOUT_FILENO 1 /* Standard output. */

[/usr]grep -r "STDERR_FILENO" include
include/boost/asio/detail/impl/handler_tracking.ipp: ::write(STDERR_FILENO, line, length);
include/unistd.h:#define STDERR_FILENO 2 /* Standard error output. */


Wait a minute. They are defined, but never used by any other std header file on my machine? I find that strange. Perhaps 0,1, and 2 are used elsewhere instead of the defined macros. The macros just exist to be referenced as an indication of how the streams are configured..?


Anyway, we can capture particular output streams in the shell like this:



./program 1> stdout.txt
./program 2> stderr.txt
./program > both.txt 2>&1


I want to create my own output stream, and capture it like this:



./program 3> mine.txt


I tried searching around unistd.h and other files included by <iostream> to see how std::cout and std::cerr worked, but as you might imagine, I got lost and confused.


I'm more interested in whether you can do this, not whether it's a good idea.



asked 33 secs ago







Can I create my own output stream that is different from stdout and stderr?

Aucun commentaire:

Enregistrer un commentaire