home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / cplus / 11615 < prev    next >
Encoding:
Text File  |  1992-07-27  |  1.3 KB  |  41 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!taumet!steve
  3. From: steve@taumet.com (Steve Clamage)
  4. Subject: Re: Question: How to get fd from cin?
  5. Message-ID: <1992Jul27.180801.573@taumet.com>
  6. Organization: TauMetric Corporation
  7. References: <4dod02XK1bYE01@JUTS.ccc.amdahl.com>
  8. Date: Mon, 27 Jul 1992 18:08:01 GMT
  9. Lines: 30
  10.  
  11. gordie@duts.ccc.amdahl.com (Gordon Freedman) writes:
  12.  
  13. >How do I get the integer fd for cin (or any iostream for that matter)? I want
  14. >to use it for XtAddInput and/or select system call.
  15.  
  16. An iostream will only have an fd if its streambuf is some sort of filebuf.
  17.  
  18. You get the streambuf* from the rdbuf() member of the ios base class.
  19. You need an unsafe downcast to filebuf* of the streambuf*, and you
  20. can then use the fd() member function of the filebuf.  Example:
  21.  
  22. #include <fstream.h>    // includes <iostream.h>
  23. main()
  24. {
  25.     cout <<
  26.      ((filebuf*)(cerr.rdbuf()))->fd() // get the fd for cerr
  27. //        ^^^^^^^^^^ unsafe downcast
  28.      << "\n";
  29.     return 0;
  30. }
  31.  
  32. If the streambuf is not really a filebuf, this will produce poor results.
  33.  
  34. Please also note that an integer file descriptor is a Unix-like notion,
  35. and may not be supported on all systems.  That is, a given implementation
  36. might not have the fd() function, or it might not return anything useful.
  37. -- 
  38.  
  39. Steve Clamage, TauMetric Corp, steve@taumet.com
  40. Vice Chair, ANSI C++ Committee, X3J16
  41.