Solved

Redirecting streams

Posted on 2003-11-11
2
345 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now