| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1334 人关注过本帖
标题:[求助]不明白cerr 与cout 的区别
只看楼主 加入收藏
cpluslover
Rank: 1
等 级:新手上路
威 望:1
帖 子:91
专家分:0
注 册:2006-12-13
收藏
 问题点数:0 回复次数:8 
[求助]不明白cerr 与cout 的区别

今天好象很倒霉的样子

随便在书上看点什么东西 都有会遇到问题

下面是潜能书上的例子,我看不出来这个cerr与 cout
的区别啊

改成cout也运行完全一样

//**********************
//** ch19_1.cpp **
//**********************

#include<iostream.h>

void fn(int a, int b)
{
if(b==0)
cerr <<"zero encountered. "
<<"The message cannot be redirected";
else
cout <<a/b <<endl;
}

void main()
{
fn(20,2);
fn(20,0);
}

请讲一讲啊
先谢谢了

搜索更多相关主题的帖子: cout cerr void int 
2007-05-03 19:29
aipb2007
Rank: 8Rank: 8
来 自:CQU
等 级:贵宾
威 望:40
帖 子:2879
专家分:7
注 册:2007-3-18
收藏
得分:0 
cout上标准输出流
cerr是标准错误流


本质上一样的吧,都是ostream,这样不同的定义只是种区分吧!

我这样理解的,不知道是不是真有什么地方不一样,仅做参考哈!

Fight  to win  or  die...
2007-05-03 19:42
yuyunliuhen
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:1435
专家分:0
注 册:2005-12-12
收藏
得分:0 

[QUOTE]cerr
Specifies the cerr global stream.
extern ostream cerr;

Return Value
An ostream object.

Remarks
The object controls unbuffered insertions to the standard error output as a byte stream. Once the object is constructed, the expression cerr.flags & unitbuf is nonzero.

Example:
// iostream_cerr.cpp
// compile with: /EHsc
// By default, cerr and clog are the same as cout
#include <iostream>
#include <fstream>
using namespace std;
void TestWide( )
{
int i = 0;
wcout << L"Enter a number: ";
wcin >> i;
wcerr << L"test for wcerr" << endl;
wclog << L"test for wclog" << endl;
}
int main( )
{
int i = 0;
cout << "Enter a number: ";
cin >> i;
cerr << "test for cerr" << endl;
clog << "test for clog" << endl;
TestWide( );
}
Input
3
1
Sample Output
Copy Code
Enter a number: 3
test for cerr
test for clog
Enter a number: 1
test for wcerr
test for wclog
[/QUOTE]


clog:
extern ostream clog;
The object controls buffered insertions to the standard error output as a byte stream.

cout:The object controls insertions to the standard output as a byte stream.

呵呵,不用翻译了吧,很易懂的。平时多查查MSDN,很有帮助的,对英语也是一种提高哦

[此贴子已经被作者于2007-5-3 20:35:09编辑过]


Go confidently in the  directions of your dreams,live the life you have imagined!Just do it!
It is no use learning without thinking!
2007-05-03 20:25
cpluslover
Rank: 1
等 级:新手上路
威 望:1
帖 子:91
专家分:0
注 册:2006-12-13
收藏
得分:0 
我的ENGLISH
有你那么好就好了

“运行正确”的程序不见得是高质量的程序
2007-05-03 21:20
zkkpkk
Rank: 2
等 级:论坛游民
威 望:5
帖 子:489
专家分:28
注 册:2006-6-17
收藏
得分:0 
一样的吧,只不够CERR一般用在错误输出上

Viva,espana!
2007-05-03 22:19
leeco
Rank: 4
等 级:贵宾
威 望:10
帖 子:1029
专家分:177
注 册:2007-5-10
收藏
得分:0 
sample shows everything.

#include <iostream>
using namespace std;

int main()
{
int a,b;
freopen(\"in.txt\",\"r\",stdin);
freopen(\"out.txt\",\"w\",stdout);
cin>>a>>b;
if(b!=0){
cout<<\"a/b=\"<<a/b<<endl;
}
else {
cerr<<\"b can't be zero.\"<<endl;
}
}

2007-05-16 15:52
raulxxyuer
Rank: 1
等 级:新手上路
威 望:1
帖 子:178
专家分:0
注 册:2007-4-23
收藏
得分:0 

有看过的呀


我很沒用,總是學不會遺忘,總是學不會割捨本不屬於我的東西。
2007-05-16 16:01
I喜欢c
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:64
帖 子:1749
专家分:0
注 册:2007-3-2
收藏
得分:0 

你把cerr和cout换个位子试试....


 我是指针,却丢失了目标地址!          我是循环,却缺少了结束条件!      我是函数,却没有人来调用!   
2007-05-16 17:31
PcrazyC
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:5652
专家分:0
注 册:2006-10-20
收藏
得分:0 
1.cout可以重定向,而cerr不能重定向.只能定向到屏幕

2.cout先储存在缓存区,而cerr没有缓存区,直接输出.

雁无留踪之意,水无取影之心
2007-05-16 17:40
快速回复:[求助]不明白cerr 与cout 的区别
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012887 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved