Solved

Automatic indentation with output streams...

Posted on 1997-10-14
4
244 Views
Last Modified: 2012-06-27
It is often desirable to add certain indentation strings at the start of each line of output of a program. A straight-forward application of this is parallel programming where it can be used to separate the output of the different processes involved.

Is there a standard method using output streams to do this
(e.g. using manipulators?).

Another way to do this is to derive a class ofstream_with_indent from ofstream, and to redefine the appropriate functions. The problem with this approach is that we do not know which functions to redefine and how.

The solution must be such that no existing code must be be rewritten (e.g. many classes already have their own ostream& operator<<).
0
Comment
Question by:brakkee011297
[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
4 Comments
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1171339
perhaps you can override endl to write an end of line and then do the indent?

0
 

Author Comment

by:brakkee011297
ID: 1171340
Yes, this is what we are doing right now. We have a special
class ostream_with_indent, which can be constructed from
an ostream& and an indentation string, and forwards all its
operations to the given ostream. The en-masse forwarding of
operator<< from ostream_with_indent to ostream is realized through a template function. We handle manipulators ourselves,
and handle endl separately.

Deriving ostream_with_indent from ostream does not work because
ostreams cannot be copied.

0
 
LVL 3

Accepted Solution

by:
EricKaplan earned 200 total points
ID: 1171341
I think you need to override the "ostream& write(const char *s, streamsize n)" method.

-Eric.
0
 

Author Comment

by:brakkee011297
ID: 1171342
Your proposal will only work when write() is a virtual function
of class ostream and if we derive ostream_with_indent from ostream.

However, as I mentioned in the explanation, deriving
a class from ostream is not possible because ostreams cannot be
copied. Therefore, we wrote a new class and are delegating operations to the ostream class.
We construct this class with an ostream& and an indentation string.

0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

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