Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Automatic indentation with output streams...

Posted on 1997-10-14
4
Medium Priority
?
248 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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…
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 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.

596 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