Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!swrinde!cs.utexas.edu!ut-emx!nowhere!sking From: sking@nowhere.uucp (Steven King) Newsgroups: comp.lang.c++ Subject: Re: How do I convert from ostream to FILE? Keywords: ostream iostream FILE Message-ID: <1991May17.041817.1618@nowhere.uucp> Date: 17 May 91 04:18:17 GMT References: <1991May16.011218.3161@agate.berkeley.edu> <732@taumet.com> Organization: American Anarchist Union Lines: 44 In article <732@taumet.com> steve@taumet.com (Stephen Clamage) writes: >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. > >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; > } > A few minor quibbles... The library distributed with cfront 2.0 has cin & cout instances of istream_withassign and ostream_withassign, repectively, and neither is defined as derived from filebuf or have a filebuf as member. ( I presume that the actual initialization of cin, etc. uses a filebuf ) Objects derived from fstreambase ( ie. ofstream and ifstream ) have the member function rdbuf() which returns a pointer to a filebuf, rendering the cast unnecessary, ie: ifstream ins ; int fd = ins.rdbuf() -> fd() ; -- If it don't stick, stink, or sting It ain't from Texas. ..!cs.utexas.edu!ut-emx!nowhere!sking