Solved

Redirecting streams

Posted on 2003-11-11
2
349 Views
Last Modified: 2010-04-01
I have a log class that uses C-style I/O. This allows me to have the following situation (simplified):

class Log
{
public:
   ...
   void SetFile(FILE* f) { file = f; }
   ...
private:
   FILE* file;
};

So I can redirect the log output to any file (including stdout or stderr for example) and change it however i want at runtime. I was trying to convert this to use C++ stream I/O and I can't seem to find an equally elegant solution that would allow me to write to a file or cout/cerr etc and change it at runtime. All I can come up with is something like the following:

class Log
{
public:
   Log(const char* filename) : os(NULL), file(filename) {
      if (file)
         os.rdbuf(file.rdbuf());
   }
   Log(std::ostream& _os) : os.rdbuf(_os.rdbuf()) { }

   void SetFile(const char* filename) {
      if (file.is_open())
         file.close();
      file.open(filename);
      if (file)
         os.rdbuf(file.rdbuf());
   }
   void SetStream(std::ostream& _os) {
      if (file.is_open())
         file.close();
      os.rdbuf(_os.rdbuf());
   }
   ...
private:
   std::ostream os;
   std::ofstream file;
};

which is ok, but having to have both an ostream and an ofstream jerks me a little. I tried having just the ofstream but you can't change its rdbuf (at least not using the iostreams library in VC7). I also tried having an ostream* but this isn't really much better overall (although it too seems to work). Can someone enlighten me with a nicer solution? Yes this is a bit pedantic for a log class but hey it might come up somewhere more important one day ;)

Cheers
James
0
Comment
Question by:JLevick
[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
2 Comments
 
LVL 4

Accepted Solution

by:
havman56 earned 125 total points
ID: 9722086


why cant u used insertion operation on filestream

std::ofstream *file_ptr;
(*file_ptr)<<(*istringstream).rdbuf();

There are lot overloaded function << for output streams u can use appropriately rather than setfunction of urs.

havman56



0
 

Author Comment

by:JLevick
ID: 9745634
thanks - my crappy docs didn't list all the overloads of the insertion operator, there is indeed one that does the job.
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

707 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