Can anyone please tell me what are the usage differences between std::cerr and perror
void perror ( const char * str );
I wonder which one is preferable in C++ applications and why it's preferable.
http://www.cplusplus.com/reference/cstdio/perror/
perror and cerr are different things. cerr - is object of std::ostream class connected with stderr. And perror prints errno and your string in stderr.
Your question basically boils down to iostream vs stdio. A similar question has been answered here.
If you're working in C++ cerr is definitely preferable to perror unless you want to do something very specific. The only real difference is that cerr is virtually the same as
fprintf(stderr, const char*, arg1, ...);
while perror will also load and display the appropriate error message depending on errno. Also you can't display variables with perror so you can't do
perror("Something went wrong, i: %d", i);
unless you preprocess your error message.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With