5

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.

Kiril Kirov
  • 37,467
  • 22
  • 115
  • 187
F. Aydemir
  • 2,665
  • 5
  • 40
  • 60

2 Answers2

3

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.

ForEveR
  • 55,233
  • 2
  • 119
  • 133
2

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.

Community
  • 1
  • 1
rath
  • 3,655
  • 1
  • 40
  • 53