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

前端 未结 5 1898
太阳男子
太阳男子 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:47

    They do rather different things.

    You use perror() to print a message to stderr that corresponds to errno. You use fprintf() to print anything to stderr, or any other stream. perror() is a very specialized printing function:

    perror(str);
    

    is equivalent to

    if (str)
        fprintf(stderr, "%s: %s\n", str, strerror(errno));
    else
        fprintf(stderr, "%s\n", strerror(errno));
    

提交回复
热议问题