When should I use perror(“…”) and fprintf(stderr, “…”)?

前端 未结 5 1892
太阳男子
太阳男子 2020-12-07 07:53

Reading the man pages and some code did not really help me in understanding the difference between - or better, when I should use - perror(\"...\") or fp

5条回答
  •  天命终不由人
    2020-12-07 08:39

    perror() always writes to stderr; strerr(), used together with fprintf(), can write to any output - including stderr but not exclusively.

    fprintf(stdout, "Error: %s", strerror(errno));
    fprintf(stderr, "Error: %s", strerror(errno)); // which is equivalent to perror("Error")
    

    Furthermore, perror imposes its own text formating "text: error description"

提交回复
热议问题