Find the answer to your Linux question:
Results 1 to 6 of 6
here the program gives a odd result: Code: #include <stdio.h> int main(){ perror("first"); perror("next"); return 0; } result: first: Success next: Illegal seek why? any resonable explanation? i found no ...
  1. #1
    ebd
    ebd is offline
    Just Joined!
    Join Date
    Aug 2009
    Posts
    2

    Question does perror() set errno?

    here the program gives a odd result:
    Code:
    #include <stdio.h>
    int main(){
        perror("first");
        perror("next");
        return 0;
    }
    result:

    first: Success
    next: Illegal seek

    why? any resonable explanation? i found no information about this in man pages.
    thanks in advance

  2. #2
    Just Joined!
    Join Date
    Jul 2009
    Posts
    10
    According to "C in a Nutshell":
    perror
    Print an error message corresponding to the value of errno

    The perror() function prints a message to the standard error stream. The output includes first the string referenced by the pointer argument, if any; then a colon and a space, then the error message that corresponds to the current value of the errno variable, ending with a newline character.

  3. #3
    ebd
    ebd is offline
    Just Joined!
    Join Date
    Aug 2009
    Posts
    2
    i know this about perror
    i wonder why the sencond perror prints an error, where the error be created?

  4. #4
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    When I copied and ran your program it produced:

    first:Success
    next:Success

    Which is what its supposed to produce. Perror just reports the value of errno...Gerard4143
    Make mine Arch Linux

  5. #5
    Just Joined!
    Join Date
    Jul 2009
    Posts
    10
    Works for me, too ...
    Code:
    joebuffer@ubuntu:~/clang$ ./test 
    first: Success
    next: Success

  6. #6
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    According to:
    When a system call fails, it usually returns -1 and sets the variable errno to a value describing what went wrong. (These values can be found in <errno.h>.) Many library functions do likewise. The function perror() serves to translate this error code into human-readable form. Note that errno is undefined after a successful library call: this call may well change this variable, even though it succeeds, for example because it internally used some other library function that failed.

    -- excerpt from perror(3): print system error message - Linux man page
    we might expect both reported results ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...