Nope. Seems an unfortunate omission. Anyone know why it was left out?
Because there's no guarantee that there *is* an underlying file descriptor
in the sense that flock expects? Because if it *was* possible to extract
the file descriptor, it might not be in the state other code expects it to
be, and even if it was it very likely wouldn't stay that way?
Consider, if you will, that fstreams generally buffer their input and
output. A naive implementation of a "get_fd" function might not guarantee
that the buffers are flushed. And then, once you have it, how do you
propose to keep the file descriptor and the stream in something
approximating the same state? The whole thing's a mess, and best avoided.
What I did once when I needed a descriptor to pass to a legacy library was
to use fopen to open the file, and create an ofstream with the constructor
that takes a descriptor. Less than optimal, for sure, but it worked for my
purposes.
I don't see a constructor that accepts an int in std:
stream.
Don't know about flock, but I recall that there are buffering issues, so
you may need to flush whenever switching between the two modes of access.
flock is defined as part of POSIX, not C or C++. Any interaction it has
with C or C++ is undefined as far as the C++ language is concerned.
However, you might be able to get away with opening the file a second time
with the POSIX open call (using the same filename) and using that handle
for flock. Just don't read or write from or to it.
Not guaranteed, of course. If you *really* need it and you want to do it
"right", use POSIX i/o for things that need flock.