Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!wuarchive!uunet!taumet!steve From: steve@taumet.com (Stephen Clamage) Newsgroups: comp.lang.c++ Subject: Re: How do I convert from ostream to FILE? Keywords: ostream iostream FILE Message-ID: <732@taumet.com> Date: 16 May 91 18:05:15 GMT References: <1991May16.011218.3161@agate.berkeley.edu> Organization: Taumetric Corporation, San Diego Lines: 45 holub@violet.berkeley.edu (Allen Holub) writes: >Is there an official way to convert from an ostream either to a FILE pointer >or file descriptor? I need to use a few C routines that take file-descriptor >arguments in a stream-based C++ application compiled under Borland C++ 2.0. You don't say whether you are using AT&T version 1.2-style (old) streams or version 2.x-style (new) iostreams. Old-style streams were built on C Standard I/O, and you can just pick up the FILE pointer. New-style iostreams do not use C Standard I/O at all (except for stdiostream, which is recommended against). So there is no FILE pointer available. Beyond that, an ostream is not necessarily file-based. It uses a streambuf-derived class for its output, and this streambuf-derived class might be a filebuf (using a file), a strstreambuf (using a char array), or some user-defined class. So there is no guarantee that an ostream will have a file associated with it. In the event that an ostream-derived class uses a filebuf (as do cout and cerr), you can use the public member function ios::rdbuf() to get the associated streambuf, then invoke public member function fd() to get the file descriptor. Of course, fd() is not a member of streambuf, but of filebuf, so you need a cast, which is not safe unless you know you are dealing with a filebuf-derived buffer class. Example: #include main() { cout << "fd for cout is " << ((filebuf*)cout.rdbuf())->fd() // UNSAFE CAST << endl; return 0; } This makes sense on systems which have the Unix-like concept of small integers representing a file as known to the operating system. (DOS is such a system.) -- Steve Clamage, TauMetric Corp, steve@taumet.com