C++中与perror相对应的流是什么?

7

可能重复:
C++替代perror()的方法

我找不到与perror等效的流。有这样的东西吗?我喜欢可以调用的事实:

perror("Error");

它将填充errno是什么。我能用流做到这点吗?


@Erik:是的!我之前看过那个函数,只是想不起来了。谢谢! - poy
2个回答

15

打印错误信息:

str << strerror(errno);

如果您在谈论流错误状态 - 那么不,您无法获得自动的有意义的错误消息。


5

由于 perror 写入到 stderr,因此C ++中的任何等效项都必须完全相同。也就是说,仅将 strerror(errno) 写入流是不够的。流本身应该(我会说必须)是一个指向标准错误的流。

以下代码片段/伪代码应该可以给您一个想法:

// depending on your compiler, this is all you need to include
#include <iostream>
#include <string.h>
#include <errno.h>

 ... somewhere in your code...

std::cerr << "Error: " << strerror(errno) << std::endl;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接