I wonder if it allowed to modify wcout. For example to write all wcout
output to a buffer during a function call, how to do this?

wchar *x;
// setup wcout to write to buffer x
my_function();
// reset wcout

thanks

Re: modifying wcout by David

David
Mon May 28 20:22:32 CDT 2007

DR wrote:
> I wonder if it allowed to modify wcout. For example to write all wcout
> output to a buffer during a function call, how to do this?
>
> wchar *x;
> // setup wcout to write to buffer x
> my_function();
> // reset wcout

Dr:

Not quite sure what you want, but maybe you should be looking at
std::wostringstream, and writing my_function() so that it takes a
wostream parameter

void my_function(std::wostream& wostrm)
{

}

Now you can pass either wcout or a wostringstream object to the function.

--
David Wilkinson
Visual C++ MVP

Re: modifying wcout by Doug

Doug
Mon May 28 21:49:54 CDT 2007

On Mon, 28 May 2007 18:52:13 -0500, DR <dr2222@yahoo.com> wrote:

>I wonder if it allowed to modify wcout. For example to write all wcout
>output to a buffer during a function call, how to do this?
>
>wchar *x;
>// setup wcout to write to buffer x
>my_function();
>// reset wcout

This should do it (wcout should be analogous):

#include <iostream>
#include <sstream>

int main()
{
std::cout << "console: Hello, world #1\n";
std::stringstream str;
std::streambuf* orig = std::cout.rdbuf(str.rdbuf());
std::cout << "str: Hello, world\n";
std::cout.rdbuf(orig);
std::cout << "console: Hello, world #2\n";
std::cout << str.rdbuf();
}

Output:

X>a
console: Hello, world #1
console: Hello, world #2
str: Hello, world

--
Doug Harrison
Visual C++ MVP

Re: modifying wcout by Ulrich

Ulrich
Tue May 29 05:15:28 CDT 2007

DR wrote:
> I wonder if it allowed to modify wcout. For example to write all wcout
> output to a buffer during a function call, how to do this?
>
> wchar *x;
> // setup wcout to write to buffer x
> my_function();
> // reset wcout

Firstly, I'd second what David said, i.e. that my_function() should take a
std::wostream&. Other than that, it is perfectly normal to redirect the
targets of the standard streams objects using rdbuf() as illustrated in
Doug's posting.

One more thing: stop using pointers where they are not needed! In above
code, 'x' is not a buffer and manually managing the memory it points to is
simply error-prone and thus IMHO stupid and a waste of time. Instead, for
general buffering, use std::vector<>. Of course, in the context of strings,
std::string or std::wstring are the appropriate tools.

Uli