?
Solved

Redirecting streams

Posted on 2003-11-11
2
Medium Priority
?
351 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 375 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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…
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 learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

752 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