Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. setw irrational behaviour

setw irrational behaviour

Scheduled Pinned Locked Moved C / C++ / MFC
debuggingvisual-studiolinuxhelpquestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Vaclav_
    wrote on last edited by
    #1

    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

    K CPalliniC 2 Replies Last reply
    0
    • V Vaclav_

      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

      K Offline
      K Offline
      k5054
      wrote on last edited by
      #2

      You do know that left applies to the output stream until its changed with either right or internal, don't you? Anyway, the only odd thing I can see is this bit

      INITIALIZE bold red text
      (offset 30 ) START PARENT PROCESS

      Your 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 next cout. 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 the left 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.

      V 1 Reply Last reply
      0
      • K k5054

        You do know that left applies to the output stream until its changed with either right or internal, don't you? Anyway, the only odd thing I can see is this bit

        INITIALIZE bold red text
        (offset 30 ) START PARENT PROCESS

        Your 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 next cout. 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 the left 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.

        V Offline
        V Offline
        Vaclav_
        wrote on last edited by
        #3

        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.

        L K 2 Replies Last reply
        0
        • V Vaclav_

          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.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          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 the setw 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 the endl manipulator.

          1 Reply Last reply
          0
          • V Vaclav_

            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.

            K Offline
            K Offline
            k5054
            wrote on last edited by
            #5

            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, so cout << 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/)

            1 Reply Last reply
            0
            • V Vaclav_

              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

              CPalliniC Offline
              CPalliniC Offline
              CPallini
              wrote on last edited by
              #6

              Quote:

              usual note that I am looking for a solution to an issue, not for commentaries on my code style

              :-D :thumbsup:

              In testa che avete, signor di Ceprano?

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups