Post by news.swva.netPost by news.swva.netHow can I "reopen" std::cin and std::cout in binary mode on unix
using GCC?
I think the answer is "you can't", since there is no way to make a c++
stream from a file descriptor. I only vaguely recall the details; I
think that perhaps one of the library implementations that I looked at
had a way to do it but it wasn't in the standard, or something.
You can create a stream buffer from an fd or FILE*; perhaps that's
what you are thinking of?
#include <cstdio>
#include <fstream>
#include <ext/stdio_filebuf.h>
int main(void)
{
// stream using a UNIX file descriptor
std::ofstream os;
__gnu_cxx::stdio_filebuf<char> fdbuf(1, std::ios::out);
os.std::ios::rdbuf(&fdbuf);
os << "Hello, world!" << std::endl;
// stream using an ISO C FILE structure
__gnu_cxx::stdio_filebuf<char> filbuf(stdout, std::ios::out);
os.std::ios::rdbuf(&filbuf);
os << "Goodbye!" << std::endl;
return 0;
}
Using the same method, you can set the stream buffer for any stream,
including std::cin and std::cout.
Regards,
Roger
- --
Roger Leigh
Printing on GNU/Linux? http://gimp-print.sourceforge.net/
Debian GNU/Linux http://www.debian.org/
GPG Public Key: 0x25BFB848. Please sign and encrypt your mail.