When to use "perror" and "fprintf"

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • L. Westmeier

    When to use "perror" and "fprintf"

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

    Any hint or help is appreciated.

    L. Westmeier

  • Richard Bos

    #2
    Re: When to use "perror&qu ot; and "fprintf&q uot;

    "L. Westmeier" <westmeier@info rmatik.hu-berlin.de> wrote:
    [color=blue]
    > Reading the man pages and some code did not really help me in
    > understanding the difference between - or better when I should use -
    > perror("...") and fprintf(stderr, "...")[/color]

    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 specialised printing function;

    perror(str);

    is equivalent to

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

    Richard

    Comment

    • Darrell Grainger

      #3
      Re: When to use &quot;perror&qu ot; and &quot;fprintf&q uot;

      On Tue, 4 May 2004, L. Westmeier wrote:
      [color=blue]
      > Reading the man pages and some code did not really help me in
      > understanding the difference between - or better when I should use -
      > perror("...") and fprintf(stderr, "...")[/color]

      The perror function works differently depending on the value of errno. If
      you use a function that effects errno then it makes sense to use perror.
      If you use a function that does not effect errno and simply returns an
      error code you should use fprintf(stderr, fmt, ...).

      For example, strtol will return LONG_MAX or LONG_MIN if a string is out of
      range and set errno to ERANGE. So if strtol fails due to out of range, I
      would use perror.

      --
      Send e-mail to: darrell at cs dot toronto dot edu
      Don't send e-mail to vice.president@ whitehouse.gov

      Comment

      • Karthik

        #4
        Re: When to use &quot;perror&qu ot; and &quot;fprintf&q uot;

        L. Westmeier wrote:[color=blue]
        > Reading the man pages and some code did not really help me in
        > understanding the difference between - or better when I should use -
        > perror("...") and fprintf(stderr, "...")
        >
        > Any hint or help is appreciated.
        >
        > L. Westmeier
        >[/color]
        They are entirely different things. No need for confusion whatsoever.

        fprintf - can be used to print to any stream.

        perror - prints the error message string, if errno is set. (Usually
        best done after a sys. call ).

        For eg, if you fail to open a directory using fopen , errno is set to
        EISDIR say.
        sometimes it would make much more sense to print the corresponding
        error message than playing with a macro like EISDIR (to the user, of
        course !!).

        --
        Karthik.
        Humans please 'removeme_' for my real email.

        Comment

        • Dan Pop

          #5
          Re: When to use &quot;perror&qu ot; and &quot;fprintf&q uot;

          In <kcQlc.185$WS3. 72@reader1.news .jippii.net> Lars Wirzenius <liw@iki.fi> writes:
          [color=blue]
          >Darrell Grainger <darrell@NOMORE SPAMcs.utoronto .ca.com>:[color=green]
          >> For example, strtol will return LONG_MAX or LONG_MIN if a string is out of
          >> range and set errno to ERANGE. So if strtol fails due to out of range, I
          >> would use perror.[/color]
          >
          >I have found that it is useful, in practice, to include the value of
          >errno in the output. These days, the error message output by perror
          >is often translated. This is good for the user, as they get an error
          >in a language they understand, but bad for me, if it is in a language
          >I do not understand. Therefore, I tend not to use perror and instead
          >use something like this:
          >
          > fprintf(stderr, "Error: Can't open file: %d: %s\n",
          > errno, strerror(errno) );
          >
          >If there is an error, I can look up the reason using the numerical
          >error code regardless of what the text looks like.[/color]

          This doesn't work for portable code, as the user's implementation need not
          use the same errno codes as yours. Call strerror(errno) twice, once in
          the user's locale, the other time in the "C" locale and compare the
          two strings. If they're identical, display only one, otherwise display
          both and ask the user to quote the English one verbatim.

          Dan
          --
          Dan Pop
          DESY Zeuthen, RZ group
          Email: Dan.Pop@ifh.de

          Comment

          • OSHIMA

            #6
            Re: When to use &quot;perror&qu ot; and &quot;fprintf&q uot;

            HI,

            I use the function 'fprintf' to show the 'Usage' message.
            if(argc != 2){
            fprintf(stderr, "Usage: %s [filename]\n", argv[0]);
            exit(1);
            }
            perror() cannot do that.

            When I have to show the standard error message and
            more information, fprintf() is very useful for me!

            "L. Westmeier" <westmeier@info rmatik.hu-berlin.de> wrote in message news:<c77rc5$q8 6$1@hahn.inform atik.hu-berlin.de>...[color=blue]
            > Reading the man pages and some code did not really help me in
            > understanding the difference between - or better when I should use -
            > perror("...") and fprintf(stderr, "...")
            >
            > Any hint or help is appreciated.
            >
            > L. Westmeier[/color]

            Comment

            Working...