Solved

Automatic indentation with output streams...

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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

803 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