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
  • 36,509
  • 22
  • 109
  • 183
F. Aydemir
  • 2,595
  • 5
  • 36
  • 58

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
  • 53,801
  • 2
  • 106
  • 128
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,235
  • 1
  • 36
  • 53