On Jul 14, 10:22 pm, Victor Bazarov <v.Abaza...@com Acast.netwrote:
James Kanze wrote:
On Jul 14, 7:24 am, Peng Yu <PengYu...@gmai l.comwrote:
It is possible to change the length of "\t" to a number
other than 8.
std::cout << "\t";
The length of "\t" had better be two on any conforming C++
system. This goes back to the very beginnings of C, and is
so simple and basic that I find it almost inconceivable that
a compiler would get it wrong.
The value of the two bytes on my systems are 9 and 0, which
is almost universal today on everything but mainframes.
(The standard requires the second byte to be 0. The first
byte will be 5, however, on an IBM mainframe.)
So where is your 8 coming from?
I believe the OP meant to describe the desire to designate a
different set of positions (number of spaces) which outputting
the TAB character skips. I don't believe C++ defines that at
all, it's the characteristic of the device with which the
output is associated.
Well, he's confirmed that, but I'll admit that the idea didn't
even occur to me. Horizontal tab is a character like any other,
and how the display device reacts to it is very, very dependent.
Practically speaking, a de facto standard of one tab stop every
eight columns has established itself today, but historically, it
really was anything goes, with the print head advancing to the
next position where someone had "configured " a tab stop (and on
some printers, the configuration consisted of moving mechanical
pieces on the printer). In the early Fortran days, 7, then
every four or five, or else only 7 and 72, were common.
Today, as I said, the de facto standard is 8, and IMHO, setting
it to anything else is more or less the equivalent of defining
operator+ to do subtraction. I rather fear that I'm not widely
followed in this opinion, however, as I am constantly running
into text which expects it to be every 2, every 3 or every 4.
Pragmatically, you can more or less assume that '\t' means
display some random number of blanks. Even if you count on 8,
and program accordingly, your output will be messed up when
someone does a diff (which inserts 2 extra characters at the
start of each line) or a grep (which inserts the filename at the
start of each line). (Historically, '\t' was used in text files
as a simple form of compression. Today, for small files, it
doesn't matter, and for big files, gzip or bzip2 are a lot more
effective. And the use of '\t' instead of spaces can interfere
with the algorithms used by gzip or bzip2, and result in less
compression.)
Anyway, the correct solution if you want to position at a
specific place in the line is to output an appropriate number of
spaces (std::setw() can help here), and not to use '\t'. The
only time you should output a '\t' is when you want a tab
character in the text, say for some external program (e.g.
make, which requires tabs in some cases), or to allow the user
to indent according to his personal preferences.
--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34