473,473 Members | 1,513 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

std::cerr

What advantages does std::cerr offer over std::cout?

(I'm trying to understand why it exists)

thanks
Jul 23 '05 #1
8 18639
Dylan wrote:
What advantages does std::cerr offer over std::cout?
There is no "advantage".
(I'm trying to understand why it exists)


It is frequently convenient to be able to separate "normal" from "abnormal" output. Thus one common use of cerr is to
log errors: i.e. one might (re)direct output to stderr (but not to stdout) to an error log, which might be a file, a
terminal screen or (my preferred option) a tannoy that squawks "ERROR! ERROR! ILLEGAL MEMORY ACCESS AT 0x038af2e2!
MISSION ABORT!".

--
Lionel B

Jul 23 '05 #2
On Tue, 7 Jun 2005 10:35:28 +0100, "Lionel B" <me@privacy.net> wrote:
Dylan wrote:
What advantages does std::cerr offer over std::cout?


There is no "advantage".
(I'm trying to understand why it exists)


It is frequently convenient to be able to separate "normal" from "abnormal" output. Thus one common use of cerr is to
log errors: i.e. one might (re)direct output to stderr (but not to stdout) to an error log, which might be a file, a
terminal screen or (my preferred option) a tannoy that squawks "ERROR! ERROR! ILLEGAL MEMORY ACCESS AT 0x038af2e2!
MISSION ABORT!".


hehe. So how do I redirect the output of std::cerr, say, to a file?
Jul 23 '05 #3
Dylan <sp******@ontheball.com> wrote:
| What advantages does std::cerr offer over std::cout?

On my OS, a common thing to do is to make programs reading from cin and
writing to cout. If foo and bar are two such programs, and I wrote

foo < infile | bar > outfile

then foo would have cin connected to the file infile, and its cout
connected to bar's cin. Bar's cout would be connected to the file
outfile.

If any of there is an error, an error message written to cerr would
appear on the screen immediately.

Cerr is not buffered, though, so excessive usage might be ineffient. If
you rather want to write information to the screen, there is also a
buffered version called clog.

summary:
cin buffered, connected to stdin
cout buffered, connected to stdout
cerr unbuffered, connected to stderr
clog buffered, connected to stderr.
--
Robert Bauck Hamar
Jul 23 '05 #4

"Dylan" <sp******@ontheball.com> wrote in message
news:nn********************************@4ax.com...

hehe. So how do I redirect the output of std::cerr, say, to a file?


The answer is very OS and compiler implementation specific, but
for an example, here's one way:

http://support.microsoft.com/default...b;en-us;110930

- Risto -
Jul 23 '05 #5


ro**********@ifi.uio.no wrote:
Dylan <sp******@ontheball.com> wrote:
| What advantages does std::cerr offer over std::cout?

On my OS, a common thing to do is to make programs reading from cin and
writing to cout. If foo and bar are two such programs, and I wrote

foo < infile | bar > outfile

then foo would have cin connected to the file infile, and its cout
connected to bar's cin. Bar's cout would be connected to the file
outfile.

If any of there is an error, an error message written to cerr would
appear on the screen immediately.

Cerr is not buffered, though, so excessive usage might be ineffient. If
you rather want to write information to the screen, there is also a
buffered version called clog.

summary:
cin buffered, connected to stdin
cout buffered, connected to stdout
cerr unbuffered, connected to stderr
clog buffered, connected to stderr.
Wow great!!. What's your OS? --
Robert Bauck Hamar


Jul 23 '05 #6
Prawit Chaivong <pr******@gmail.com> wrote:
| ro**********@ifi.uio.no wrote:
| > Dylan <sp******@ontheball.com> wrote:
| > | What advantages does std::cerr offer over std::cout?
| >
| > On my OS, a common thing to do is to make programs reading from cin and
| > writing to cout. If foo and bar are two such programs, and I wrote
| >
| > foo < infile | bar > outfile
| >
| > then foo would have cin connected to the file infile, and its cout
| > connected to bar's cin. Bar's cout would be connected to the file
| > outfile.
| >
| > If any of there is an error, an error message written to cerr would
| > appear on the screen immediately.
| >
| > Cerr is not buffered, though, so excessive usage might be ineffient. If
| > you rather want to write information to the screen, there is also a
| > buffered version called clog.
| >
| > summary:
| > cin buffered, connected to stdin
| > cout buffered, connected to stdout
| > cerr unbuffered, connected to stderr
| > clog buffered, connected to stderr.
|
| Wow great!!. What's your OS?

None of importance, really; the four objects are standard in C++, but
I use Crux <URL: http://crux.nu>, Solaris <URL: http://www.sun.com> and
Redhat Enterprise Linux <URL: http://www.redhat.com/> on a regular
basis.

To another question in this thread: I can redirect stderr to a file
with

foo 2> errmsg

(for a program foo and a file errmsg.)
--
Robert Bauck Hamar
Jul 23 '05 #7
>> hehe. So how do I redirect the output of std::cerr, say, to a file?
The answer is very OS and compiler implementation specific


Really? Re-assigning stream buffers is a portable operation:

#include <fstream>
#include <iostream>

int main()
{
std::ofstream log("somefile");
std::streambuf *save = std::cerr.rdbuf();

// Redirect stream buffers
if (log.is_open())
std::cerr.rdbuf(log.rdbuf());

std::cout << "This is a test message\n";
std::cerr << "This is a test message\n";

// Restore cerr's stream buffer before terminating
if (log.is_open())
std::cerr.rdbuf(save);
}

Jul 23 '05 #8
Lionel B wrote:
Dylan wrote:
What advantages does std::cerr offer over std::cout?

There is no "advantage".

(I'm trying to understand why it exists)

It is frequently convenient to be able to separate "normal" from "abnormal" output. Thus one common use of cerr is to
log errors: i.e. one might (re)direct output to stderr (but not to stdout) to an error log, which might be a file, a
terminal screen or (my preferred option) a tannoy that squawks "ERROR! ERROR! ILLEGAL MEMORY ACCESS AT 0x038af2e2!
MISSION ABORT!".


Also, cerr is also unbuffered, whereas cout is buffered. clog exists,
and (as I understand it) is a buffered version of cerr.

Jul 23 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Mike - EMAIL IGNORED | last post by:
I have a VC++6.0 program than calls library functions that write to cerr. I would like to capture this output in the calling program. Would you please let me know how I could to this? If I...
2
by: Trevor | last post by:
Hello, Please bear with me, I am trying to learn C++. I am implementing some error/debug functions which format a message and output it to a C++ stream. I would like to design it using one low...
3
by: GGG | last post by:
I have a situation where at tool is passing me a large array of strings that I need to process in a particular type of data. Each item in the array gets to me as a pair of std::strings, basically,...
2
by: Agoston Bejo | last post by:
Hi, how can one redirect std::cerr into a file? Specifically, if I have an ofstream object, is there something I can do to make "cerr << ..." calls write into that particular ofstream object? ...
8
by: sandwich_eater | last post by:
I get compiler error "cerr undeclared first use this function." #define err_ret(e) cerr << e; return -1 .... err_ret("who ate my muffy?");
11
by: FNX | last post by:
I'm stumbling around a little bit here trying to get a feel for wide/Unicode functionality. I'm pretty up on C++ in general, streaming stuff not so much. If I run the following code, I get a...
3
by: Jim Langston | last post by:
I have a computer game that has no console. If I want to output something I push a string to the back of a std::vector<std::stringand that gets output and maintained. All is well and good, but...
5
by: Pradeep | last post by:
Hi All, I am facing some problem using istream_iterator for reading the contents of a file and copying it in a vector of strings.However the same thing works for a vector of integers. The...
4
by: toton | last post by:
Hi, I want to have a log stream, where I can put certain logs. The log can be console or file or any other ui widget. This part I had already done with. However I want log stream (std::ostream...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.