Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading a file descriptor

 
vin Hari
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,
Please tell me is there way to read the <b>file descriptors</b> and give the contents to the <b>streambuf</b>,
using streams,
iam trying from all the way to achieve this but not able to succeed please tell me answer to this problem basically
iam trying in this way,


in this way but this is not giving me answer because of

this casting,
please help me how to read this file descriptor and give the result to streambuf ,
which other part of my project is using ,please help.
iam using c++ for this if this is not a right forum please transfer this to the corresponding forum, because almost c++ is also a object oriented as java thats why i posted this here because reamins the same.
[ November 26, 2007: Message edited by: vin Hari ]
 
Joe Ess
Bartender
Pie
Posts: 9264
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This forum is for Java-specific IO questions. I'll move this question to the Unix forum. It looks like your previous question got good traffic there.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The ifstream constructor is expecting the name of a file to open; casting an int file descriptor to a char* isn't going to magically give you the name of the file (of course). There's actually no way to do what you want built into the standard C++ libraries. You can either "roll your own" by subclassing streambuf (not recommended) or use a third-party library. You can't go wrong with the Boost libraries, many of which are being rolled into future C++ standards. Boost.iostreams has a file_descriptor_source class that lets you do what you want:

http://www.boost.org/libs/iostreams/doc/classes/file_descriptor.html
 
vin Hari
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you for reply ,
so according to this discussion is there no way to open that file descriptor
and redirect the output to the streambuf, is it the conclusion,
if not in c++,also in c,\
even i tried directly
ifstream f(fd);
which result in error. i included <fstream.h>.

please clear me.
[ November 26, 2007: Message edited by: vin Hari ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually I said yes, there is a way. I think what you would do would be to implement a custom subclass of streambuf, which would use low-level C calls to work with the file descriptor. I imagine you could copy some code from the ifstream class to do this.

Or...

You could simply use the Boost library, which already provides a class (file_descriptor_source) which does exactly what you need. See the link I provided above.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic