Solved

redirecting cerr

Posted on 1999-01-26
7
286 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 how to clear a vector as well as how to detect empty vectors in C++.

688 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