home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!taumet!steve
- From: steve@taumet.com (Steve Clamage)
- Subject: Re: Question: How to get fd from cin?
- Message-ID: <1992Jul27.180801.573@taumet.com>
- Organization: TauMetric Corporation
- References: <4dod02XK1bYE01@JUTS.ccc.amdahl.com>
- Date: Mon, 27 Jul 1992 18:08:01 GMT
- Lines: 30
-
- gordie@duts.ccc.amdahl.com (Gordon Freedman) writes:
-
- >How do I get the integer fd for cin (or any iostream for that matter)? I want
- >to use it for XtAddInput and/or select system call.
-
- An iostream will only have an fd if its streambuf is some sort of filebuf.
-
- You get the streambuf* from the rdbuf() member of the ios base class.
- You need an unsafe downcast to filebuf* of the streambuf*, and you
- can then use the fd() member function of the filebuf. Example:
-
- #include <fstream.h> // includes <iostream.h>
- main()
- {
- cout <<
- ((filebuf*)(cerr.rdbuf()))->fd() // get the fd for cerr
- // ^^^^^^^^^^ unsafe downcast
- << "\n";
- return 0;
- }
-
- If the streambuf is not really a filebuf, this will produce poor results.
-
- Please also note that an integer file descriptor is a Unix-like notion,
- and may not be supported on all systems. That is, a given implementation
- might not have the fd() function, or it might not return anything useful.
- --
-
- Steve Clamage, TauMetric Corp, steve@taumet.com
- Vice Chair, ANSI C++ Committee, X3J16
-