f



How can I "reopen" std::cin and std::cout in binary mode?

How can I "reopen" std::cin and std::cout in binary mode on unix using GCC?
If you know how to do that with MS VC++ it would be even awesome!!!
thanks,
Ivan
0
news
5/7/2005 9:07:54 AM
comp.unix.programmer 10848 articles. 0 followers. kokososo56 (349) is leader. Post Follow

5 Replies
8617 Views

Similar Articles

[PageSpeed] 48

On Sat, 07 May 2005 05:07:54 -0400, news.swva.net 
  <user@example.net> wrote:
> How can I "reopen" std::cin and std::cout in binary mode on unix using GCC?
> If you know how to do that with MS VC++ it would be even awesome!!!
> thanks,
> Ivan

In unix there is no difference beween text and binary mode (unless that 
is something specific to C++).

-- 
Test-tube babies shouldn't throw stones.
0
Bill
5/8/2005 5:54:09 AM
> How 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.  (I
think I was trying to make a c++ stream connected to a socket.) Perhaps
someone else will know the details.

Luckily you can substitute a "no-op", since there is no difference on
Unix.

> If you know how to do that with MS VC++ it would be even awesome!!!

Try another group.


--Phil.

0
phil_gg04
5/8/2005 4:21:41 PM
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

phil_gg04@treefic.com writes:

>> How 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.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.0 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.8 <http://mailcrypt.sourceforge.net/>

iD8DBQFCflg4VcFcaSW/uEgRAuWmAJ9NGEWcajMhIoytRtibXjkxuCpXIQCg6ia+
VmKbc0rzdM9HSbdPwVWAdcA=
=Wzz0
-----END PGP SIGNATURE-----
1
Roger
5/8/2005 6:19:38 PM
phil gg04 writes:

>> How 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.  (I
> think I was trying to make a c++ stream connected to a socket.) Perhaps
> someone else will know the details.

You can find some details here:

http://www.ginac.de/~kreckel/fileno/

-- 
--Jhair

PGP key available from public servers - ID: 0xBAA600D0
 
0
Jhair
5/12/2005 11:19:35 AM
* "news.swva.net" <user@example.net>
| How can I "reopen" std::cin and std::cout in binary mode on unix using GCC?
| If you know how to do that with MS VC++ it would be even awesome!!!

With VC++, you could try to link against binmode.obj (included with
the compiler), which switches the regular streams to binary mode.

Don't know whether the C++ streams are affected by this, and whether
binmode.obj can be used with gcc on windows.

R'
0
Ralf
5/12/2005 3:27:55 PM
Reply: