setw irrational behaviour
-
This is a corollary on an old issue of cout and perror (cerr) output not following expected sequence. I understand that is caused by cout and perror(cerr) outputting to DIFFERENT streams. End of story for now. Now I am having issues with inserting setw and left into cout NOT doing what I expect. See attached IDE output. (Align cout text left starting on column set in setw.) setw(30) without left is also being totally ignored, until width is set to 75 (?). What gives? PS in Linux if it should make a difference. To avoid any unnecessary replies - usual note that I am looking for a solution to an issue, not for commentaries on my code style. Cheers
else if (child > 0) { /\* This is the parent. \*/ //cout << setw(75) << left << setw(75) << left << "\\033\[1;31mINITIALIZE bold red text\\033\[0m\\n"; cout << setw(75) << left << "\\033\[1;31mINITIALIZE bold red text\\n" << flush; //cout << setw(75) << left << setw(75) << left << "\\033\[1;31mRESET bold red text\\033\[0m\\n"; // cout << setw(75) << left << setw(75) << left << "\\033\[1;32mbold green text\\033\[0m\\n"; cout << flush; // in << "" << endl; cout << setw(75) << left << "(offset 30 ) START PARENT PROCESS " << endl;
#ifdef DEBUG
cout << setw(75) << left
<< "parent has / knows child process ID " << dec
<< child << endl;
cout << setw(75) << left
<< "TRACE This is the parent, orignal process "
<< endl;
cout << setw(75) << left << "TASK @line " << dec << __LINE__
<< dec << endl;
cout << setw(75) << left << "function " << __FUNCTION__ << endl;
cout << setw(75) << left << "STOP @line " << __LINE__ << endl;
cout << setw(75) << left
<< "child > 0 File descriptor socket[0] " << dec
<< sockets[0] << endl;
cout << setw(75) << left
<< "child > 0 File descriptor socket[1] " << dec
<< sockets[1] << endl;
#endif
// why closing the other socket (?)Actual output on IDE console
START test area 370
main
TASK create a pair of connected sockets
TRACE @line 403
SUCCESS opening stream socket pair
result 0
socketpair File descriptor socket[0] 11
socketpair File descriptor socket[1] 12
TRACE @line 420
TASK fork Create another process child default -1
TASK @line 427
function main
TRACE @line 429
INITIALIZE bold red text
(offset 30 ) START PARENT PROCESS -
This is a corollary on an old issue of cout and perror (cerr) output not following expected sequence. I understand that is caused by cout and perror(cerr) outputting to DIFFERENT streams. End of story for now. Now I am having issues with inserting setw and left into cout NOT doing what I expect. See attached IDE output. (Align cout text left starting on column set in setw.) setw(30) without left is also being totally ignored, until width is set to 75 (?). What gives? PS in Linux if it should make a difference. To avoid any unnecessary replies - usual note that I am looking for a solution to an issue, not for commentaries on my code style. Cheers
else if (child > 0) { /\* This is the parent. \*/ //cout << setw(75) << left << setw(75) << left << "\\033\[1;31mINITIALIZE bold red text\\033\[0m\\n"; cout << setw(75) << left << "\\033\[1;31mINITIALIZE bold red text\\n" << flush; //cout << setw(75) << left << setw(75) << left << "\\033\[1;31mRESET bold red text\\033\[0m\\n"; // cout << setw(75) << left << setw(75) << left << "\\033\[1;32mbold green text\\033\[0m\\n"; cout << flush; // in << "" << endl; cout << setw(75) << left << "(offset 30 ) START PARENT PROCESS " << endl;
#ifdef DEBUG
cout << setw(75) << left
<< "parent has / knows child process ID " << dec
<< child << endl;
cout << setw(75) << left
<< "TRACE This is the parent, orignal process "
<< endl;
cout << setw(75) << left << "TASK @line " << dec << __LINE__
<< dec << endl;
cout << setw(75) << left << "function " << __FUNCTION__ << endl;
cout << setw(75) << left << "STOP @line " << __LINE__ << endl;
cout << setw(75) << left
<< "child > 0 File descriptor socket[0] " << dec
<< sockets[0] << endl;
cout << setw(75) << left
<< "child > 0 File descriptor socket[1] " << dec
<< sockets[1] << endl;
#endif
// why closing the other socket (?)Actual output on IDE console
START test area 370
main
TASK create a pair of connected sockets
TRACE @line 403
SUCCESS opening stream socket pair
result 0
socketpair File descriptor socket[0] 11
socketpair File descriptor socket[1] 12
TRACE @line 420
TASK fork Create another process child default -1
TASK @line 427
function main
TRACE @line 429
INITIALIZE bold red text
(offset 30 ) START PARENT PROCESSYou do know that
left
applies to the output stream until its changed with eitherright
orinternal
, don't you? Anyway, the only odd thing I can see is this bitINITIALIZE bold red text
(offset 30 ) START PARENT PROCESSYour code is:
cout << setw(75) << left
<< "\033[1;31mINITIALIZE bold red text\n" << flush;
cout << flush;
cout << setw(75) << left << "(offset 30 ) START PARENT PROCESS "
<< endl;In that first line, you have a terminating
\n
, so the output stream sends your text, including the line terminator, then fills the output so that 75 chars are actually written, since the cout string is 32 chars long, you get an additional 43 spaces to make up the output width before the nextcout
. Remove the terminating\n
and add a<< endl
. Otherwise, it all looks good to me. Changing the width to 35 seems to do the right thing, and removing theleft
right-justifies the output, which I think is correct, so I'm not sure what you're seeing that's wrong. Maybe give us an example of what output you get when it seems incorrect to you, please. -
You do know that
left
applies to the output stream until its changed with eitherright
orinternal
, don't you? Anyway, the only odd thing I can see is this bitINITIALIZE bold red text
(offset 30 ) START PARENT PROCESSYour code is:
cout << setw(75) << left
<< "\033[1;31mINITIALIZE bold red text\n" << flush;
cout << flush;
cout << setw(75) << left << "(offset 30 ) START PARENT PROCESS "
<< endl;In that first line, you have a terminating
\n
, so the output stream sends your text, including the line terminator, then fills the output so that 75 chars are actually written, since the cout string is 32 chars long, you get an additional 43 spaces to make up the output width before the nextcout
. Remove the terminating\n
and add a<< endl
. Otherwise, it all looks good to me. Changing the width to 35 seems to do the right thing, and removing theleft
right-justifies the output, which I think is correct, so I'm not sure what you're seeing that's wrong. Maybe give us an example of what output you get when it seems incorrect to you, please.PLEASE DISREGARD ALL OF THE FOLLOWING. I AM USING SETW INCORRECTLY. I need to check this more, but ... with cout << set(x) << left << "TEXT " << data << endl; .. I expect TEXT to be output starting at column x and left justified. As of now the data is output at unexpected column - as you pointed out. Did I misinterpret setw - as it appears to set the width of the entire cout message , not just setting the starting column? The "flush" ( clear cout buffer ) should have no effect on the actual output, it is there as an attempt to fix another issue. However - endl at each cout line should also clear the cout buffer , hence each line should start independently - which it does. The whole snippet is part of TWO processes output and I need to try this setw in single process FIRST! It appears that cout does not run in each process independently, so let me eliminate the child process. I am also not sure if "\n" clears the cout buffer.
-
PLEASE DISREGARD ALL OF THE FOLLOWING. I AM USING SETW INCORRECTLY. I need to check this more, but ... with cout << set(x) << left << "TEXT " << data << endl; .. I expect TEXT to be output starting at column x and left justified. As of now the data is output at unexpected column - as you pointed out. Did I misinterpret setw - as it appears to set the width of the entire cout message , not just setting the starting column? The "flush" ( clear cout buffer ) should have no effect on the actual output, it is there as an attempt to fix another issue. However - endl at each cout line should also clear the cout buffer , hence each line should start independently - which it does. The whole snippet is part of TWO processes output and I need to try this setw in single process FIRST! It appears that cout does not run in each process independently, so let me eliminate the child process. I am also not sure if "\n" clears the cout buffer.
The
setw()
manipulator (<iomanip> functions | Microsoft Docs[^]) just sets the minimum width of the next field to be output. So whatever is in that field will be output (after converting to text if necessary) in a field that is at least that many characters wide. If the text is wider than thesetw
value then it will overflow the field. As to your other comments above,setw
has nothing to do with which column the output will start at.Vaclav_ wrote:
It appears that cout does not run in each process independently
It sort of does, but ultimately they both feed into the same stream that is managed by the console handler.
Vaclav_ wrote:
I am also not sure if "\n" clears the cout buffer.
No, it is just a character sent to the stream, but I think the console handler converts it to
CR LF
. The end of output is sensed by the presence of theendl
manipulator. -
PLEASE DISREGARD ALL OF THE FOLLOWING. I AM USING SETW INCORRECTLY. I need to check this more, but ... with cout << set(x) << left << "TEXT " << data << endl; .. I expect TEXT to be output starting at column x and left justified. As of now the data is output at unexpected column - as you pointed out. Did I misinterpret setw - as it appears to set the width of the entire cout message , not just setting the starting column? The "flush" ( clear cout buffer ) should have no effect on the actual output, it is there as an attempt to fix another issue. However - endl at each cout line should also clear the cout buffer , hence each line should start independently - which it does. The whole snippet is part of TWO processes output and I need to try this setw in single process FIRST! It appears that cout does not run in each process independently, so let me eliminate the child process. I am also not sure if "\n" clears the cout buffer.
Vaclav_ wrote:
Did I misinterpret setw - as it appears to set the width of the entire cout message , not just setting the starting column?
Looks like you figured out where you went wrong with
setw()
, but I'd like to point out that setw sets the width of the next output field, but does not truncate, socout << setw(5) << "Hello World";
will still print all of the string "Hello World". If you really want to put output at a specific location on screen, maybe take a look at curses: [http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/\](http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/) -
This is a corollary on an old issue of cout and perror (cerr) output not following expected sequence. I understand that is caused by cout and perror(cerr) outputting to DIFFERENT streams. End of story for now. Now I am having issues with inserting setw and left into cout NOT doing what I expect. See attached IDE output. (Align cout text left starting on column set in setw.) setw(30) without left is also being totally ignored, until width is set to 75 (?). What gives? PS in Linux if it should make a difference. To avoid any unnecessary replies - usual note that I am looking for a solution to an issue, not for commentaries on my code style. Cheers
else if (child > 0) { /\* This is the parent. \*/ //cout << setw(75) << left << setw(75) << left << "\\033\[1;31mINITIALIZE bold red text\\033\[0m\\n"; cout << setw(75) << left << "\\033\[1;31mINITIALIZE bold red text\\n" << flush; //cout << setw(75) << left << setw(75) << left << "\\033\[1;31mRESET bold red text\\033\[0m\\n"; // cout << setw(75) << left << setw(75) << left << "\\033\[1;32mbold green text\\033\[0m\\n"; cout << flush; // in << "" << endl; cout << setw(75) << left << "(offset 30 ) START PARENT PROCESS " << endl;
#ifdef DEBUG
cout << setw(75) << left
<< "parent has / knows child process ID " << dec
<< child << endl;
cout << setw(75) << left
<< "TRACE This is the parent, orignal process "
<< endl;
cout << setw(75) << left << "TASK @line " << dec << __LINE__
<< dec << endl;
cout << setw(75) << left << "function " << __FUNCTION__ << endl;
cout << setw(75) << left << "STOP @line " << __LINE__ << endl;
cout << setw(75) << left
<< "child > 0 File descriptor socket[0] " << dec
<< sockets[0] << endl;
cout << setw(75) << left
<< "child > 0 File descriptor socket[1] " << dec
<< sockets[1] << endl;
#endif
// why closing the other socket (?)Actual output on IDE console
START test area 370
main
TASK create a pair of connected sockets
TRACE @line 403
SUCCESS opening stream socket pair
result 0
socketpair File descriptor socket[0] 11
socketpair File descriptor socket[1] 12
TRACE @line 420
TASK fork Create another process child default -1
TASK @line 427
function main
TRACE @line 429
INITIALIZE bold red text
(offset 30 ) START PARENT PROCESS