Solved

Automatic indentation with output streams...

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

708 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

17 Experts available now in Live!

Get 1:1 Help Now