This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
ext/stdio_filebuf
- From: Daniel Franke <franke dot daniel at gmail dot com>
- To: gcc-help at gcc dot gnu dot org
- Date: Sat, 5 Aug 2006 22:48:12 +0200
- Subject: ext/stdio_filebuf
Hi all.
I wanted to employ C++ streams on posix file descriptors obtained by pipe().
The extension class stdio_filebuf serves exactly this purpose.
There are two non-default constructors for stdio_filebuf, one takes a posix
file descriptior, the other one a pointer to FILE. The docs [1] for the first
constructor state:
"This constructor associates a file stream buffer with an open POSIX file
descriptor. The file descriptor will be automatically closed when the
stdio_filebuf is closed/destroyed."
For the second:
"This constructor associates a file stream buffer with an open C FILE*.
The FILE* will not be automatically closed when the stdio_filebuf is
closed/destroyed."
Why was the closing behaviour upon destruction implemented differently?
Assume a function as
void transform(int infd, int outfd) {
stdio_filebuf<char> inbuf(infd, std::ios::in);
std::istream in(&inbuf);
stdio_filebuf<char> outbuf(outfd, std::ios::out);
std::ostream out(&outbuf);
// read from in, write to out
}
where in infd/outfd may either be pipes, files, STD*_FILENO constants or
anything else that is suitable. Using stdio_filebuf on infd=STDIN_FILENO or
outfd=STDOUT_FILENO will close stdin/stdout of the current process as soon as
the in and out streams are destroyed -- one would like to avoid that.
A blog [2] indicates that there are buffering problems of some kind if the
constructor for POSIX file descriptors is used and proposes a workaround
using the FILE* based constructor. Are these problems known?
For now, I'm resorting to posix read/write functions instead of stdio_filebuf.
Regards
Daniel
[1]
http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/class____gnu__cxx_1_1stdio__filebuf.html
[2] http://synflood.at/blog/index.php?/plugin/tag/c%2B%2B