<< "\t";

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael

    << "\t";

    just a quickie:

    as
    cout << endl;
    is to
    cout << "\n";

    is there an equivalent for
    cout << "\t";

    ??
    Thanks
    Mike


  • David Harmon

    #2
    Re: &lt;&lt; &quot;\t&quo t;;

    On Fri, 14 May 2004 15:46:29 +0000 (UTC) in comp.lang.c++, "Michael"
    <slick_mick_00@ hotmail.com> wrote,
    [color=blue]
    >as
    >cout << endl;
    >is to
    >cout << "\n";
    >
    >is there an equivalent for
    >cout << "\t";[/color]

    No! And if there was, it would be even more evil than cout<<endl;

    Comment

    • Jean Charbonneau

      #3
      Re: &lt;&lt; &quot;\t&quo t;;


      "Michael" <slick_mick_00@ hotmail.com> wrote in message
      news:c82pkl$7rk $1@hercules.bti nternet.com...[color=blue]
      > just a quickie:
      >
      > as
      > cout << endl;
      > is to
      > cout << "\n";
      >
      > is there an equivalent for
      > cout << "\t";
      >
      > ??
      > Thanks
      > Mike
      >
      >[/color]

      cout << endl; is not the same as cout << "\n"; because the first one besides
      doing a new line, flushes the output as well


      Comment

      • Howard

        #4
        Re: &lt;&lt; &quot;\t&quo t;;


        "David Harmon" <source@netcom. com.invalid> wrote in message
        news:40c0ead7.8 3159306@news.we st.earthlink.ne t...[color=blue]
        > On Fri, 14 May 2004 15:46:29 +0000 (UTC) in comp.lang.c++, "Michael"
        > <slick_mick_00@ hotmail.com> wrote,
        >[color=green]
        > >as
        > >cout << endl;
        > >is to
        > >cout << "\n";
        > >
        > >is there an equivalent for
        > >cout << "\t";[/color]
        >
        > No! And if there was, it would be even more evil than cout<<endl;[/color]

        What's evil about endl? I use it a lot. It's especially helpful when
        debugging stuff, because it flushes the buffer and I know that there's
        nothing in the stream that I can't yet see in my console window.

        -Howard


        Comment

        • Ivan Vecerina

          #5
          Re: &lt;&lt; &quot;\t&quo t;;

          "Michael" <slick_mick_00@ hotmail.com> wrote in message
          news:c82pkl$7rk $1@hercules.bti nternet.com...[color=blue]
          > just a quickie:
          >
          > as
          > cout << endl;
          > is to
          > cout << "\n";[/color]

          First of all, one should prefer to use
          cout << '\n' instead of cout << "\n"
          (avoiding unnecessary pessimization).

          Then, the purpose of endl is to end a line AND
          flush the output buffer. It is equivalent to:
          cout << '\n';
          cout.flush();
          This is useful in several logging or communication
          protocols (although the console output streams,
          by default, are not buffered in standard C++).
          [color=blue]
          > is there an equivalent for
          > cout << "\t";[/color]
          NB: again, prefer '\t'

          How often does one want to flush the output
          after writing a tab character ?



          Regards,
          Ivan
          --
          http://ivan.vecerina.com/contact/?subject=NG_POST <- e-mail contact form


          Comment

          • Marc

            #6
            Re: &lt;&lt; &quot;\t&quo t;;

            "Howard" wrote:
            [color=blue]
            > What's evil about endl? I use it a lot. It's especially helpful when
            > debugging stuff, because it flushes the buffer and I know that there's
            > nothing in the stream that I can't yet see in my console window.[/color]

            Why not something like:

            #ifdef DEBUG
            std::cout.rdbuf ().setbuf(0,0);
            #endif

            ?

            Comment

            • Howard

              #7
              Re: &lt;&lt; &quot;\t&quo t;;


              "Marc" <MarcDotGlisse@ Loria.Fr> wrote in message
              news:c82scj$141 d$1@nef.ens.fr. ..[color=blue]
              > "Howard" wrote:
              >[color=green]
              > > What's evil about endl? I use it a lot. It's especially helpful when
              > > debugging stuff, because it flushes the buffer and I know that there's
              > > nothing in the stream that I can't yet see in my console window.[/color]
              >
              > Why not something like:
              >
              > #ifdef DEBUG
              > std::cout.rdbuf ().setbuf(0,0);
              > #endif
              >
              > ?
              >[/color]

              Why? It's a lot more work than using endl instead of '\n' (especially with
              a lot of cout's being used), and you haven't given any indication as to what
              if anything is wrong with endl.

              Also, just because I'm "debugging" , doesn't mean it's a debug build. Often
              times there are problems that only show up in release builds, and it helps a
              lot to be sure that everything that was written to the stream was flushed to
              the console prior to the problem occurring that I'm trying to solve.

              Again...what's wrong with endl???

              -Howard


              Comment

              • John Harrison

                #8
                Re: &lt;&lt; &quot;\t&quo t;;


                "Michael" <slick_mick_00@ hotmail.com> wrote in message
                news:c82pkl$7rk $1@hercules.bti nternet.com...[color=blue]
                > just a quickie:
                >
                > as
                > cout << endl;
                > is to
                > cout << "\n";
                >
                > is there an equivalent for
                > cout << "\t";
                >
                > ??
                > Thanks
                > Mike
                >[/color]

                No, but it's easy enough to write your own. Call it 'endt' say

                #include <iostream>
                using namespace std;

                ostream& endt(ostream& os)
                {
                return os << '\t' << flush;
                }

                int main()
                {
                cout << "abc" << endt;
                cout << "def" << endl;
                }

                john


                Comment

                • Ivan Vecerina

                  #9
                  Re: &lt;&lt; &quot;\t&quo t;;

                  "Howard" <alicebt@hotmai l.com> wrote in message
                  news:Aa6pc.5758 9$Ut1.1517942@b gtnsc05-news.ops.worldn et.att.net...[color=blue]
                  > What's evil about endl? I use it a lot. It's especially helpful when
                  > debugging stuff, because it flushes the buffer and I know that there's
                  > nothing in the stream that I can't yet see in my console window.[/color]

                  Note: console I/O is unbuffered in standard C++.
                  So std::endl should be unnecessary, though not really evil IMO.

                  Regards,
                  Ivan
                  --
                  http://ivan.vecerina.com/contact/?subject=NG_POST <- e-mail contact form



                  Comment

                  • John Carson

                    #10
                    Re: &lt;&lt; &quot;\t&quo t;;

                    "John Harrison" <john_andronicu s@hotmail.com> wrote in message
                    news:2gke8fF3va 89U1@uni-berlin.de[color=blue]
                    >
                    > No, but it's easy enough to write your own. Call it 'endt' say
                    >
                    > #include <iostream>
                    > using namespace std;
                    >
                    > ostream& endt(ostream& os)
                    > {
                    > return os << '\t' << flush;
                    > }
                    >
                    > int main()
                    > {
                    > cout << "abc" << endt;
                    > cout << "def" << endl;
                    > }
                    >
                    > john[/color]


                    Nice. And if you don't really want the flushing, it is easier still.

                    #include <iostream>
                    using namespace std;

                    const char tab='\t';

                    int main()
                    {
                    cout << "abc" << tab;
                    cout << "def" << endl;
                    }


                    --
                    John Carson
                    1. To reply to email address, remove donald
                    2. Don't reply to email address (post here instead)

                    Comment

                    • Wouter Lievens

                      #11
                      Re: &lt;&lt; &quot;\t&quo t;;

                      "Michael" <slick_mick_00@ hotmail.com> schreef in bericht
                      news:c82pkl$7rk $1@hercules.bti nternet.com...[color=blue]
                      > just a quickie:
                      >
                      > as
                      > cout << endl;
                      > is to
                      > cout << "\n";
                      >
                      > is there an equivalent for
                      > cout << "\t";
                      >
                      > ??
                      > Thanks
                      > Mike[/color]


                      std::endl is a function, not a character.


                      Comment

                      • Jeff Schwab

                        #12
                        Re: &lt;&lt; &quot;\t&quo t;;

                        Ivan Vecerina wrote:[color=blue]
                        > "Howard" <alicebt@hotmai l.com> wrote in message
                        > news:Aa6pc.5758 9$Ut1.1517942@b gtnsc05-news.ops.worldn et.att.net...
                        >[color=green]
                        >>What's evil about endl? I use it a lot. It's especially helpful when
                        >>debugging stuff, because it flushes the buffer and I know that there's
                        >>nothing in the stream that I can't yet see in my console window.[/color]
                        >
                        >
                        > Note: console I/O is unbuffered in standard C++.[/color]

                        Where does the standard say that?
                        [color=blue]
                        > So std::endl should be unnecessary, though not really evil IMO.
                        >
                        > Regards,
                        > Ivan[/color]

                        Comment

                        • Ivan Vecerina

                          #13
                          Re: &lt;&lt; &quot;\t&quo t;;

                          "Jeff Schwab" <jeffplus@comca st.net> wrote in message
                          news:hdqdnXmNIu _BqzXdRVn-ig@comcast.com. ..[color=blue]
                          > Ivan Vecerina wrote:[color=green]
                          > > "Howard" <alicebt@hotmai l.com> wrote in message
                          > > news:Aa6pc.5758 9$Ut1.1517942@b gtnsc05-news.ops.worldn et.att.net...
                          > >[color=darkred]
                          > >>What's evil about endl? I use it a lot. It's especially helpful when
                          > >>debugging stuff, because it flushes the buffer and I know that there's
                          > >>nothing in the stream that I can't yet see in my console window.[/color]
                          > >
                          > > Note: console I/O is unbuffered in standard C++.[/color]
                          >
                          > Where does the standard say that?[/color]

                          I slipped here, my comment was not totally correct.

                          cerr is unbuffered, and should indeed be used for debugging stuff.

                          Regarding cout, it is a bit more complicated... it is unlikely to
                          be buffered at the C++ level because by default it is supposed
                          to be synchronized with stdout. But it can still be buffered.

                          The following post/thread provides a description of the situation:



                          Thanks, and best regards,
                          Ivan
                          --
                          http://ivan.vecerina.com/contact/?subject=NG_POST <- e-mail contact form


                          Comment

                          • Jeff Schwab

                            #14
                            Re: &lt;&lt; &quot;\t&quo t;;

                            Ivan Vecerina wrote:[color=blue]
                            > "Jeff Schwab" <jeffplus@comca st.net> wrote in message
                            > news:hdqdnXmNIu _BqzXdRVn-ig@comcast.com. ..
                            >[color=green]
                            >>Ivan Vecerina wrote:
                            >>[color=darkred]
                            >>>"Howard" <alicebt@hotmai l.com> wrote in message
                            >>>news:Aa6pc.5 7589$Ut1.151794 2@bgtnsc05-news.ops.worldn et.att.net...
                            >>>
                            >>>
                            >>>>What's evil about endl? I use it a lot. It's especially helpful when
                            >>>>debugging stuff, because it flushes the buffer and I know that there's
                            >>>>nothing in the stream that I can't yet see in my console window.
                            >>>
                            >>>Note: console I/O is unbuffered in standard C++.[/color]
                            >>
                            >>Where does the standard say that?[/color]
                            >
                            >
                            > I slipped here, my comment was not totally correct.
                            >
                            > cerr is unbuffered, and should indeed be used for debugging stuff.
                            >
                            > Regarding cout, it is a bit more complicated... it is unlikely to
                            > be buffered at the C++ level because by default it is supposed
                            > to be synchronized with stdout. But it can still be buffered.[/color]

                            Why would that make it unlikely to be buffered? I've never seen a
                            situation in which cout was unbuffered.

                            Comment

                            • Ivan Vecerina

                              #15
                              Re: &lt;&lt; &quot;\t&quo t;;

                              "Jeff Schwab" <jeffplus@comca st.net> wrote in message
                              news:S7-dnbJoKZuG2TfdRV n-ig@comcast.com. ..[color=blue][color=green]
                              > > Regarding cout, it is a bit more complicated... it is unlikely to
                              > > be buffered at the C++ level because by default it is supposed[/color][/color]
                              emphasis on: ~~~~~~~~~~~~~~~ ~[color=blue][color=green]
                              > > to be synchronized with stdout. But it can still be buffered.[/color]
                              >
                              > Why would that make it unlikely to be buffered? I've never seen a
                              > situation in which cout was unbuffered.[/color]

                              Please read the thread I refered to in my previous post:


                              Unless the program calls std::ios_base:: sync_with_stdio (false),
                              it is possible to mix calls that write to stdout and cout with
                              the guarantee of a properly synchronized output.
                              I.e: cout<<'a'; putchar('b'); cout<<'c'<<endl ;
                              Is required to output "abc" (and not "bac" or anything else).

                              Because, on most platforms, cout is implemented in terms of stdout,
                              this means that buffering must be disabled in cout - at the C++ level.
                              An implementation-defined buffering mechanism of stdout may still
                              apply, however.

                              Regards,
                              Ivan
                              --
                              http://ivan.vecerina.com/contact/?subject=NG_POST <- e-mail contact form



                              Comment

                              Working...