我知道两者都可以作为错误消息,
perror(" ");
fprintf(stderr, " ");
但是它们之间真正的区别是什么?各自的优点/缺点是什么?
最佳答案
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:
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>.
Then an error message string followed by a <newline>.
The contents of the error message strings shall be the same as those returned by strerror() with argument errno.
所以,
perror( " " );
会发出类似的东西
: invalid argument
到 stderr
,具体取决于 errno
的当前值。
但是
fprintf( stderr, " " );
将仅向 stderr
发出空格字符,而不打印当前 errno
值的字符串表示形式。
fprintf( stderr, "");
单独来看是非常无用的。 perror( "");
将提供有关 errno
当前值的数据。
关于c - perror 和 fprintf 到 stderr 之间有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40677589/