[lug] C++, fstream, finding name of open stream

D. Stimits stimits at idcomm.com
Fri Dec 7 15:34:06 MST 2001


Tkil wrote:
> 
> >>>>> "DS" == D Stimits <stimits at idcomm.com> writes:
> 
> DS> Somehow it seems a bit silly though that a file stream doesn't
> DS> have a means of uniquely identifying itself the way a file
> DS> descriptor would (I can do an fstat on a descriptor...I'd love an
> DS> overloaded fstat() that accepted an fstream in place of a
> DS> descriptor). I can live without it, but life would be simpler with
> DS> it.
> 
> look at fstream::rdbuf, which returns a filebuf (derived from
> streambuf), which has a "fd()" method.  so, something like this should
> work:
> 
>    ifstream is;
>    int file_descriptor = is.rdbuf->fd();
> 
> as for portability ... no idea.  probably not very -- but then again,
> if you're thinking in terms of inodes, how portable are you being?
> 
> t.
> _______________________________________________
> Web Page:  http://lug.boulder.co.us
> Mailing List: http://lists.lug.boulder.co.us/mailman/listinfo/lug

In this particular case, portability doesn't matter, though it would be
nice if it runs on FreeBSD and a few of the UNIX clones.

The rdbuf seems to be just what I need, then I can stat via the
descriptor. This leads me to some more interesting ideas as well, it is
possible to declare a filebuf and an istream separately, then attach the
filebuf to the istream later. I'm thinking of making something as a
substitute for a filebuf that is derived the same way, but uses non-file
sources for input: postgresql_buf or mysql_buf. Under the inheritance
diagrams I found in Stroustrup, the fstream is just an istream attached
to the filebuf; unfortunately, there isn't any mention of the
inheritance or structure of filebuf itself. I'm thinking probably
filebuf is a typedef for something like basic_stream<char>, or something
that is derived from basic_stream<char> to provide file-specific
customizations. I have a copy of a slightly older draft standard, which
shows some of the operations on a filebuf, but it fails to mention what
it is derived from. Does anyone here know of a web site that has a
complete inheritance/class type diagram of C++ streams and buffers? Or
better yet, the whole std:: namespace? If not, can anyone tell me the
inheritance and typedefs associated with a filebuf, so I might be able
to write my own version that is a dropin replacement for non-file
sources? I could really stand to pick up a streams book, but can't
afford to ATM. What would be really impressive is if the whole class
relation set could be found in a big postscript poster I could zoom in
on and pan around (hey, I may as well be greedy if I'm going to ask for
too much anyway).

D. Stimits, stimits at idcomm.com



More information about the LUG mailing list