Solved

redirecting cerr

Posted on 1999-01-26
7
284 Views
Last Modified: 2010-04-16
How do I redirect cerr to a string var instead of printing to the screen????
0
Comment
Question by:meetze
  • 3
  • 3
7 Comments
 
LVL 1

Expert Comment

by:newexpert
ID: 1185147
You don't redirect cerr.  You create a strstream that's associated with a char* buffer or a stringstream associated with a c++ string.

eg
char buf[256];
strstream mystream(buf);
mystream << "Your output";

0
 

Author Comment

by:meetze
ID: 1185148
but i want to redirect the errors messages from the screen to a variable....
0
 
LVL 22

Expert Comment

by:nietod
ID: 1185149
I believe--I'm not sure this is allowed--you can use the init() procedure to specify a new stream buffer for a stream.  With this you can make cerr use a basic_stringbuf that you declare.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:meetze
ID: 1185150
if you could give me and example of how to rredirect it i would accept your answer....
0
 
LVL 22

Accepted Solution

by:
nietod earned 30 total points
ID: 1185151
For 20pts, you don't get too much....

A little more reasearch shows that the rdbuf() function can be used for this  

Try something like

// at start of main.
basic_streambuf<char> NewBuf;
basic_streambuf<char> *OldBufPtr = cerr.rdbuf(&NewBuf);

//cerr now uses  NewBuf.

// At end of main.
cerr.rdbuf(OldBufPtr);
// cerr now uses its original stream buffer.  


0
 

Author Comment

by:meetze
ID: 1185152
i am running low on points....thanks
0
 
LVL 22

Expert Comment

by:nietod
ID: 1185153
That's okay.  Its just that for 20pts, I don't like to start doing research and testing in an area I'm not famialr with.  I just answered because it looked like it wasn't going to get any more help and I thought it was better than nothing.
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question