C语言 将 perror 输出重定向到 fprintf(stderr, " ")

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5483120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 08:13:07  来源:igfitidea点击:

Redirect perror output to fprintf(stderr, " ")

cstderrerrno

提问by kingsmasher1

In case a system call function fails, we normally use perror to output the error message. I want to use fprintf to output the perror string. How can I do something like this:

如果系统调用函数失败,我们通常使用 perror 输出错误消息。我想使用 fprintf 输出 perror 字符串。我怎么能做这样的事情:

fprintf(stderr, perror output string here);

回答by maverik

#include <errno.h>

fprintf(stderr, "%s\n", strerror(errno));

Note: strerror doesn't apply \nto the end of the message

注意:strerror 不适\n用于消息的结尾