Solved

Automatic indentation with output streams...

Posted on 1997-10-14
4
243 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

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!

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 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.

726 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