Skip to content

Instantly share code, notes, and snippets.

@rajatkhanduja
Created March 10, 2012 19:49
Show Gist options
  • Save rajatkhanduja/2012695 to your computer and use it in GitHub Desktop.
Save rajatkhanduja/2012695 to your computer and use it in GitHub Desktop.
Converting a file descriptor to an istream or ostream (with g++-4.5) [ non-portable method ]
#include <iostream>
#include <ext/stdio_filebuf.h>
using __gnu_cxx::stdio_filebuf;
using std::istream;
using std::ostream;
inline stdio_filebuf<char> * fileBufFromFD (int fd, std::_Ios_Openmode mode)
{
return (new stdio_filebuf<char> (fd, mode));
}
istream * createInStreamFromFD (int fd)
{
stdio_filebuf<char> * fileBuf = fileBufFromFD (fd, std::ios::in);
return (new istream (fileBuf));
}
ostream * createOutStreamFromFD (int fd)
{
stdio_filebuf<char> * fileBuf = fileBufFromFD (fd, std::ios::out);
return (new ostream (fileBuf));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment