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

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > May 2005 > How can I "reopen" std::cin and std::cout in binary mode?





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author How can I "reopen" std::cin and std::cout in binary mode?
news.swva.net

2005-05-07, 7:51 am

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
Bill Marcum

2005-05-08, 2:48 am

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.
phil_gg04@treefic.com

2005-05-08, 5:50 pm

> 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.

Roger Leigh

2005-05-08, 5:50 pm

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

phil_gg04@treefic.com writes:

> 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/ uEgRAuWmAJ9NGEWcajMhIoytRtibXjkxuCpXIQCg
6ia+
VmKbc0rzdM9HSbdPwVWAdcA=
=Wzz0
-----END PGP SIGNATURE-----
Jhair Tocancipa Triana

2005-05-12, 7:55 am

phil gg04 writes:

[vbcol=seagreen]
> 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

Ralf Fassel

2005-05-12, 5:52 pm

* "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'
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com