Changing std::cout?

G

genxtech

Is it possible to change how many characters are printed to the
standard output when "\t" is passed to cout?
 
A

Armen Tsirunyan

Is it possible to change how many characters are printed to the
standard output when "\t" is passed to cout?

I think no, how \t is interpreted depends on the operating system, I
guess.
 
Ö

Öö Tiib

Is it possible to change how many characters are printed to the
standard output when "\t" is passed to cout?

'\t' is a single character. cout is output stream. If you want to pass
some other characters to cout, then pass.
 
G

genxtech

'\t' is a single character. cout is output stream. If you want to pass
some other characters to cout, then pass.

I understand that it is a single character, but since it represents a
tab in the output stream... I was wondering if there is a parameter to
set in the <iostream> set of classes that allow for the manipulation
of how many characters are outputted ... regardless of the os that is
used, and with out hard coding the number of spaces.
 
R

red floyd

I understand that it is a single character, but since it represents a
tab in the output stream... I was wondering if there is a parameter to
set in the<iostream> set of classes that allow for the manipulation
of how many characters are outputted ... regardless of the os that is
used, and with out hard coding the number of spaces.

Nope. It's system specific. There's no guarantee that your system will
even convert tabs to spaces on output.

You'll have to ask in a newsgroup dedicated to your platform.
 
J

Juha Nieminen

red floyd said:
Nope. It's system specific.

Actually that's not entirely true. You could write your own streambuffer
which converts '\t' characters into spaces and make std::cout use that.
 
J

Jens Thoms Toerring

I understand that it is a single character, but since it represents a
tab in the output stream... I was wondering if there is a parameter to
set in the <iostream> set of classes that allow for the manipulation
of how many characters are outputted

But '\t' is just a single character, so it can only be output
as one character. There's another program that receives the
output from stdout and that gets the single tab character and
interprets it. This may e.g. a console program or a shell that
then expands the tab. Or stdout may have been redirected to a
file, and if you afterward check what's in the file (e.g. with
a hex editor) you will find that there's just a single '\t'
character. Only if you view that file with some program this
viewer progam will expand the tab into a number of spaces. So
the conversion of the tab doesn't happen during the 'cout <<'
but only when another program (which you don't know about from
within your progam) interprets the '\t'.

... regardless of the os that is
used, and with out hard coding the number of spaces.

To achieve what you want '\t' would have to be converted already
in the 'cout <<' call, but that's not what's happening and I am
not aware of any possibility to get it to do that. It also would
be rather difficult to implement: the internals of the ostream
would have to keep track at which position in a line it is to be
able to calculate into how many spaces a tab has to be converted.
But it's not so easy to do that since there are other characters
like '\b' ('backspace') that make things more interesting. Has
the internal line position counter to be decremented at a '\b'
or incremented? And what's about the '\a' ('bell')? Does it
count as a character? (Or should 'cout <<' try to make the
computer beep and not output it at all?) Like them '\t' is a
non-printable character that is used as a "signal" to whatever
is reading on stdout and it's that reader that only knows what
to do wih them. So the most reasonable way seems to me to leave
the output alone and let the program that reads the output deal
with all that - it may have some options to tell it into how
many spaces a tab is to be expanded. If it doesn't you could
pipe the output through a program that does suitable tab re-
placement...
Regards, Jens
 
M

Marcel Müller

Hi,
It also would
be rather difficult to implement: the internals of the ostream
would have to keep track at which position in a line it is to be
able to calculate into how many spaces a tab has to be converted.
But it's not so easy to do that since there are other characters
like '\b' ('backspace') that make things more interesting. Has
the internal line position counter to be decremented at a '\b'
or incremented? And what's about the '\a' ('bell')? Does it
count as a character? (Or should 'cout <<' try to make the
computer beep and not output it at all?) Like them '\t' is a
non-printable character that is used as a "signal" to whatever
is reading on stdout and it's that reader that only knows what
to do wih them.

if a custom stream wants to virtualize \t it must do the same for all
control characters. In this case most of the questions are solved.


Marcel
 
G

genxtech

But '\t' is just a single character, so it can only be output
as one character. There's another program that receives the
output from stdout and that gets the single tab character and
interprets it. This may e.g. a console program or a shell that
then expands the tab. Or stdout may have been redirected to a
file, and if you afterward check what's in the file (e.g. with
a hex editor) you will find that there's just a single '\t'
character. Only if you view that file with some program this
viewer progam will expand the tab into a number of spaces. So
the conversion of the tab doesn't happen during the 'cout <<'
but only when another program (which you don't know about from
within your progam) interprets the '\t'.

 ... regardless of the os that is


To achieve what you want '\t' would have to be converted already
in the 'cout <<' call, but that's not what's happening and I am
not aware of any possibility to get it to do that. It also would
be rather difficult to implement: the internals of the ostream
would have to keep track at which position in a line it is to be
able to calculate into how many spaces a tab has to be converted.
But it's not so easy to do that since there are other characters
like '\b' ('backspace') that make things more interesting. Has
the internal line position counter to be decremented at a '\b'
or incremented? And what's about the '\a' ('bell')? Does it
count as a character? (Or should 'cout <<' try to make the
computer beep and not output it at all?) Like them '\t' is a
non-printable character that is used as a "signal" to whatever
is reading on stdout and it's that reader that only knows what
to do wih them. So the most reasonable way seems to me to leave
the output alone and let the program that reads the output deal
with all that - it may have some options to tell it into how
many spaces a tab is to be expanded. If it doesn't you could
pipe the output through a program that does suitable tab re-
placement...
                                 Regards, Jens

Thanks, now I understand, now I get it.
 
R

red floyd

Is it possible to change how many characters are printed to the
standard output when "\t" is passed to cout?

As another note, it may not even be your OS that does this. I've
worked on dumb terminals where tabstops can be specified at the
terminal level -- Lear Siegler ADM-12.
 
G

genxtech

As another note, it may not even be your OS that does this.  I've
worked on dumb terminals where tabstops can be specified at the
terminal level -- Lear Siegler ADM-12.

I just figured out how to do that with my os. Thanks.
 
F

Frédéric JARDON

Is it possible to change how many characters are printed to the
.......

To achieve what you want '\t' would have to be converted already
in the 'cout <<' call, but that's not what's happening and I am
not aware of any possibility to get it to do that.

This is a job for the codecvt facet. You can find an example in Bjarne
Stroustrup's book (converting lower case characters to upper case) at
section: D.4.6, p905, from the "C++ Programming Language Special
Edition". This section of the book if freely available from Bjarne
Stroustrup's homepage:

http://www.research.att.com/~bs/3rd_loc.pdf

Frederic JARDON
 
J

James Kanze

Is it possible to change how many characters are printed to the
standard output when "\t" is passed to cout?

No.

First, outputting to cout doesn't print any characters, per se;
it just writes them to the open stream. And outputting '\t'
writes exactly one character, always.

If you want to expand tabs internally, it's fairly simple using
a filtering streambuf which does it however you want.
 
J

James Kanze

On 10/2/2010 10:05 PM, genxtech wrote:
Nope. It's system specific. There's no guarantee that your system will
even convert tabs to spaces on output.

Do you know of any that do? Neither Unix nor Windows do any
such conversion. Normally, the conversion is in the rendering
engine, when the text is rendered to the printer or the screen,
and not before.
 
J

James Kanze

if a custom stream wants to virtualize \t it must do the same for all
control characters. In this case most of the questions are solved.

No. A custom streambuf can do pretty much anything it wants.
I've used them to expand tabs (it only takes a couple of lines
of code if you've got the framework---e.g. boost::iostream), to
insert timestamps at the start of lines, and a number of other
things.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,825
Members
47,371
Latest member
Brkaa

Latest Threads

Top