[英]When should I use perror("...") and fprintf(stderr, "...")?
Reading the man pages and some code did not really help me in understanding the difference between - or better, when I should use - perror("...")
or fprintf(stderr, "...")
.阅读手册页和一些代码并不能真正帮助我理解 - 或者更好,什么时候应该使用 -
perror("...")
或fprintf(stderr, "...")
。
Calling perror
will give you the interpreted value of errno
, which is a thread-local error value written to by POSIX syscalls (ie, every thread has it's own value for errno
). 调用
perror
将为您提供errno
的解释值,这是由POSIX系统调用写入的线程局部错误值(即,每个线程都有自己的errno
值)。 For instance, if you made a call to open()
, and there was an error generated (ie, it returned -1
), you could then call perror
immediately afterwards to see what the actual error was. 例如,如果你调用
open()
,并且生成了一个错误(即返回-1
),那么之后你可以立即调用perror
以查看实际错误是什么。 Keep in mind that if you call other syscalls in the meantime, then the value in errno
will be written over, and calling perror
won't be of any use in diagnosing your issue if an error was generated by an earlier syscall. 请记住,如果在此期间调用其他系统调用,则
errno
的值将被覆盖,如果早期系统调用生成错误,则调用perror
对诊断问题没有任何用处。
fprintf(stderr, ...)
on the other-hand can be used to print your own custom error messages. 另一方面
fprintf(stderr, ...)
可用于打印您自己的自定义错误消息。 By printing to stderr
, you avoid your error reporting output being mixed with "normal" output that should be going to stdout
. 通过打印到
stderr
,可以避免错误报告输出与应该转到stdout
“正常”输出混合。
Keep in mind that fprintf(stderr, "%s\\n", strerror(errno))
is similar to perror(NULL)
since a call to strerror(errno)
will generate the printed string value for errno
, and you can then combined that with any other custom error message via fprintf
. 请记住,
fprintf(stderr, "%s\\n", strerror(errno))
类似于perror(NULL)
因为调用strerror(errno)
会产生印刷字符串值errno
,然后就可以结合这与fprintf
任何其他自定义错误消息。
They do rather different things. 他们做了不同的事情。
You use perror()
to print a message to stderr
that corresponds to errno
. 您使用
perror()
将消息打印到与errno
对应的stderr
。 You use fprintf()
to print anything to stderr
, or any other stream. 您使用
fprintf()
将任何内容打印到stderr
或任何其他流。 perror()
is a very specialized printing function: perror()
是一个非常专业的打印功能:
perror(str);
is equivalent to 相当于
if (str)
fprintf(stderr, "%s: %s\n", str, strerror(errno));
else
fprintf(stderr, "%s\n", strerror(errno));
perror(const char *s)
: prints the string you give it followed by a string that describes the current value of errno
. perror(const char *s)
:打印你给它的字符串,后跟一个描述errno
当前值的字符串。
stderr
: it's an output stream used to pipe your own error messages to (defaults to the terminal). stderr
:它是一个输出流,用于将自己的错误消息传递给(默认为终端)。
Relevant: 相关:
char *strerror(int errnum)
: give it an error number, and it'll return the associated error string. char *strerror(int errnum)
:给它一个错误号,它将返回相关的错误字符串。
perror() always writes to stderr; perror()总是写给stderr; strerr(), used together with fprintf(), can write to any output - including stderr but not exclusively.
strerr()与fprintf()一起使用,可以写入任何输出 - 包括stderr但不是唯一的。
fprintf(stdout, "Error: %s", strerror(errno));
fprintf(stderr, "Error: %s", strerror(errno)); // which is equivalent to perror("Error")
Furthermore, perror imposes its own text formating "text: error description" 此外,perror强制自己的文本格式化“文本:错误描述”
Perror函数需要更多时间来执行执行调用从用户空间到内核空间frasf调用goest到api到kernal
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.