简体   繁体   English

perror和fprintf to stderr有什么区别?

[英]What's difference between perror and fprintf to stderr?

I know both can work as error messages, 我知道两者都可以作为错误消息,

perror(" ");
fprintf(stderr, " ");

but what are really differences between them? 但是它们之间真正的区别是什么? What are advantages/disadvantages of each ? 每个优点/缺点是什么?

Per the perror() standard : 根据perror()标准

The perror() function shall map the error number accessed through the symbol errno to a language-dependent error message, which shall be written to the standard error stream as follows: perror()函数必须将通过符号errno访问的错误号映射到与语言有关的错误消息,该消息应按以下方式写入标准错误流:

  • First (if s is not a null pointer and the character pointed to by s is not the null byte), the string pointed to by s followed by a <colon> and a <space>. 首先(如果s不是空指针,并且s指向的字符不是空字节),则由s指向的字符串后跟<colon>和<space>。

  • Then an error message string followed by a <newline>. 然后是错误消息字符串,后跟<newline>。

The contents of the error message strings shall be the same as those returned by strerror() with argument errno. 错误消息字符串的内容应与带有参数errno的strerror()返回的内容相同。

So, 所以,

perror( " " );

will emit something like 会发出类似

 : invalid argument

to stderr , depending on the current value of errno . stderr ,取决于errno的当前值。

But

fprintf( stderr, " " );

will emit only a space character to stderr and not print the string representation of the current errno value. 只会向stderr发出一个空格字符,而不会打印当前errno值的字符串表示形式。

fprintf( stderr, " " ); in isolation is pretty useless. 孤立是没有用的。 perror( " " ); will provide data about the current value of errno . 将提供有关errno当前值的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM