Josuttis has given an example of creating a user-defined stream buffer that can be initialized with a file descriptor, so as to write to an arbitrary destination, which may be a file, socket, etc. ("The C++ Standard Library", 2nd Edition, pg 835)
This buffer can be used to initialize an output stream.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
class fdoutbuf : public std::streambuf
{
protected:
int fd; // file descriptor
public:
fdoutbuf(int _fd) :
fd(_fd)
{
}
/// ...
}
class fdostream : public std::ostream
{
protected:
fdoutbuf buf;
/// ...
}
|
Josuttis then states (pg 837):
If some other file descriptor is available - for example, for a file or a socket - it also can be used as the constructor argument.
|
The question is how to obtain a file's descriptor?
I have searched the online docs, but there is nothing available that suggests a solution.
Logically, it is possible to obtain a file descriptor only when it either created or subsequently. But how?
Note that a file descriptor is different from a FILE*.
Thanks.