"std::endl or "\n" "

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • andrew.smith.cpp@gmail.com

    "std::endl or "\n" "

    hi,
    whts the difference between the std::endl or "\n" ?
    because both do the same work

    Thanks
  • d major

    #2
    Re: "std::e ndl or "\n&quo t; "

    On Mar 29, 12:48 pm, andrew.smith... .@gmail.com wrote:
    hi,
    whts the difference between the std::endl or "\n"  ?
    because both do the same work
    >
    Thanks
    I found nothing difference between both at least in my program.
    I also want to know that.

    Comment

    • James Kanze

      #3
      Re: "std::e ndl or "\n&quo t; "

      On 29 mar, 05:48, andrew.smith... .@gmail.com wrote:
      whts the difference between the std::endl or "\n" ?
      because both do the same work
      std::endl flushes the buffer, "\n" doesn't. If you don't know
      what you're doing, and don't want to think about it, use
      std::endl. It will make debugging a lot easier. If you're
      outputting a lot of short lines in a sequence, however, you
      probably want to use "\n", since flushing can be a fairly
      expensive proposition. (My general rule is to use "\n" in loops
      or short sequences of code, but to make sure that there is an
      std::endl or an explicit call to flush at the end of the
      function or after the loop.)

      --
      James Kanze (GABI Software) email:james.kan ze@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

      Comment

      • James Kanze

        #4
        Re: "std::e ndl or "\n&quo t; "

        On 29 mar, 07:22, pauldepst...@at t.net wrote:
        On Mar 29, 12:48 pm, andrew.smith... .@gmail.com wrote:
        whts the difference between the std::endl or "\n" ?
        because both do the same work
        Not all differences are visible. The difference is that std::endl
        flushes the stream's buffer and \n doesn't.
        Which is a very visible difference if your program crashes
        immediately after, or if you don't do anything to flush the
        buffer immediately.

        Ideally, you'd consider both and choose the appropriate one each
        time. Practically, for most smaller programs, just using
        std::endl everywhere is the simplest solution. (If you're
        generating a file of a couple of million lines, however, I
        wouldn't recommend it.)

        --
        James Kanze (GABI Software) email:james.kan ze@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

        Comment

        • =?ISO-8859-1?Q?Andr=E9_Castelo?=

          #5
          Re: "std::e ndl or "\n&quo t; "

          On Mar 29, 12:48 pm, andrew.smith... .@gmail.com wrote:
          whts the difference between the std::endl or "\n" ?
          because both do the same work
          Not all differences are visible. The difference is that std::endl
          flushes the stream's buffer and \n doesn't.
          What are the advantages of flushing the stream buffer?? Does it mean
          that the ocasional '\n' from input will be discarded or am i missing
          something?

          Comment

          • Obnoxious User

            #6
            Re: "std::e ndl or "\n&quo t; "

            On Sat, 29 Mar 2008 07:03:29 -0700, André Castelo wrote:
            On Mar 29, 12:48 pm, andrew.smith... .@gmail.com wrote:
            whts the difference between the std::endl or "\n" ? because both
            do the same work
            Not all differences are visible. The difference is that std::endl
            flushes the stream's buffer and \n doesn't.
            >
            What are the advantages of flushing the stream buffer?? Does it mean
            that the ocasional '\n' from input will be discarded or am i missing
            something?
            When you flush the stream buffer you're telling it to write what
            ever it holds and clear it. If it doesn't get flushed, you won't see it
            on your terminal or in your file.

            --
            OU

            Comment

            • Rolf Magnus

              #7
              Re: "std::e ndl or "\n&quo t; "

              André Castelo wrote:
              On Mar 29, 12:48 pm, andrew.smith... .@gmail.com wrote:
              whts the difference between the std::endl or "\n" ?
              because both do the same work
              Not all differences are visible. The difference is that std::endl
              flushes the stream's buffer and \n doesn't.
              >
              What are the advantages of flushing the stream buffer?? Does it mean
              that the ocasional '\n' from input will be discarded or am i missing
              something?
              No. Writing I/O is usually faster if you write several bytes at once instead
              of sending each to its target seperately, be it for file I/O or the
              standard output streams, since there is (on most systems) a relatively
              large fixed overhead for writing something. Therefore, the streams buffer
              data internally and then send it to its target as a block, either when the
              buffer is full, or when you explicitly flush the stream.

              Comment

              • Juha Nieminen

                #8
                Re: "std::e ndl or "\n&quo t; "

                André Castelo wrote:
                What are the advantages of flushing the stream buffer?? Does it mean
                that the ocasional '\n' from input will be discarded or am i missing
                something?
                If you are, for example, writing thousands of lines of text to a file,
                using "\n" instead of std::endl is much faster because the stream is not
                flushed after each line.

                If you are printing a few lines of text to standard output, std::endl
                may be a better idea because it forces the text to be flushed before the
                program continues.

                Comment

                • James Kanze

                  #9
                  Re: "std::e ndl or "\n&quo t; "

                  André Castelo a écrit :
                  On Mar 29, 12:48 pm, andrew.smith... .@gmail.com wrote:
                  whts the difference between the std::endl or "\n" ?
                  because both do the same work
                  Not all differences are visible. The difference is that std::endl
                  flushes the stream's buffer and \n doesn't.
                  What are the advantages of flushing the stream buffer?? Does
                  it mean that the ocasional '\n' from input will be discarded
                  or am i missing something?
                  The data isn't actually output until you flush the buffer. If
                  someone else is waiting for it, they wait. If your program
                  crashes, it never gets written.

                  --
                  James Kanze (GABI Software) email:james.kan ze@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

                  Comment

                  • Juha Nieminen

                    #10
                    Re: "std::e ndl or "\n&quo t; "

                    James Kanze wrote:
                    The data isn't actually output until you flush the buffer.
                    Except if the buffer gets full. (Or you close the file handle, or...)

                    Comment

                    Working...