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

发布于 2024-10-27 08:37:11 字数 149 浏览 10 评论 0 原文

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

fprintf(stderr, perror output string here);

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:

fprintf(stderr, perror output string here);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

∞觅青森が 2024-11-03 08:37:11
#include <errno.h>

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

注意:strerror 不会将 \n 应用于消息末尾

#include <errno.h>

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文