Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

perror and std::cerr use

Tags:

c++

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.

like image 794
F. Aydemir Avatar asked Mar 22 '13 09:03

F. Aydemir


2 Answers

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.

like image 176
ForEveR Avatar answered Oct 05 '22 22:10

ForEveR


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.

like image 23
rath Avatar answered Oct 05 '22 23:10

rath