Thread: cout, cerr

  1. #1
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246

    cout, cerr

    What is the difference between them? (if any)
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Here is a good page to answer that.

    http://www.cs.ust.hk/~martin/01Sprin...n_iostream.htm
    Sent from my iPadŽ

  3. #3
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    OK tnx

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    cerr is an error stream commad

    cout is an input stream command both are found in iostream

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    cout is an input stream?

    I gues cin is an output stream then?

    hmm...
    Sent from my iPadŽ

  6. #6
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    cerr and cout are both for output.
    cerr is especialy for error messages.
    I knew it even before opening this thread.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Then what was the point of the thread?
    Sent from my iPadŽ

  8. #8
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by SlyMaelstrom
    Then what was the point of the thread?
    He just wanted to see if we knew as well.

  9. #9
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    No, what I don't know is the difference in their functionality. Why there are different objects for normal text output and error output.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by siavoshkc
    No, what I don't know is the difference in their functionality. Why there are different objects for normal text output and error output.
    I thought Sly hit that already.
    Quote Originally Posted by SlyMaelstrom
    Here is a good page to answer that.

    http://www.cs.ust.hk/~martin/01Sprin...n_iostream.htm
    Did you follow this link?
    The difference between cout and cerr is illustrated clearly during output redirection (forget about it? Take a look here!). Only data output by cout are redirected to the file. Data output by cerr are still displayed in the terminal. This is usually what we want, because error messages should not mix with normal output which are redirected to a file in this example.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  11. #11
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    I read that and concluded that it makes no difference in output to screen. And if we have a fstream object then cerr will output the text to screen instead of file, am I right?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    How about an example?
    Code:
    #include <iostream>
    using std::cout;
    using std::cerr;
    
    int main()
    {
       for ( int i = 0; i < 10; ++i )
       {
          cout << "cout: i = " << i << '\n';
          if ( i & 1 )
          {
             cerr << "cerr: i = " << i << '\n';
          }
       }
       return 0;
    }
    Running it without redirection:
    Code:
    C:\Test\GnuC>Cpp\myapp.exe
    cerr: i = 1
    cerr: i = 3
    cerr: i = 5
    cerr: i = 7
    cerr: i = 9
    cout: i = 0
    cout: i = 1
    cout: i = 2
    cout: i = 3
    cout: i = 4
    cout: i = 5
    cout: i = 6
    cout: i = 7
    cout: i = 8
    cout: i = 9
    Redirecting stdout:
    Code:
    C:\Test\GnuC>Cpp\myapp.exe > output.txt
    cerr: i = 1
    cerr: i = 3
    cerr: i = 5
    cerr: i = 7
    cerr: i = 9
    The contents of output.txt:
    Code:
    cout: i = 0
    cout: i = 1
    cout: i = 2
    cout: i = 3
    cout: i = 4
    cout: i = 5
    cout: i = 6
    cout: i = 7
    cout: i = 8
    cout: i = 9
    Redirecting stderr:
    Code:
    C:\Test\GnuC>Cpp\myapp.exe 2> errors.txt
    cout: i = 0
    cout: i = 1
    cout: i = 2
    cout: i = 3
    cout: i = 4
    cout: i = 5
    cout: i = 6
    cout: i = 7
    cout: i = 8
    cout: i = 9
    The contents of errors.txt:
    Code:
    cerr: i = 1
    cerr: i = 3
    cerr: i = 5
    cerr: i = 7
    cerr: i = 9
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  13. #13
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Aha, I didn't know about redirection and when I pressed the link(in sly's link) it was about makefile!!
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. class methods to cout stream
    By shintaro in forum C++ Programming
    Replies: 5
    Last Post: 11-11-2008, 07:27 PM
  2. Cout, Cerr, and Clog
    By g4j31a5 in forum C++ Programming
    Replies: 9
    Last Post: 07-10-2007, 08:13 PM
  3. cout
    By RenderedAwake in forum C++ Programming
    Replies: 5
    Last Post: 02-03-2005, 07:14 AM
  4. How Does cout Work
    By jrahhali in forum C++ Programming
    Replies: 8
    Last Post: 08-25-2004, 03:19 PM
  5. FAQ cout
    By evilmonkey in forum FAQ Board
    Replies: 1
    Last Post: 10-07-2001, 11:32 AM