简体   繁体   中英

How does one check the meaning of a numerical return value in C under Linux?

Suppose I'm invoking write() and it returns with the error -8. How can I check which one of the documented error return values this stands for?

You'd usually use

  man functionname

or perhaps

  man -S2 functionname # specify other section (2, 3, 5, 7)

Most functions that return 0 on success, nonnull on failure, document that they also set errno . You can get the meaning of the errno global variable 1

  • by looking at errno.h
  • by using the strerror function ( #include <string.h> )
  • or use the convenient helper perror that prints an error message based on strerror and the prefix given:

      perror("oops, popen failed with: "); 

1 well, it doesn't technically need to be a global variable. Depending on your OS/library it may be a macro, a threadlocal variable, both etc...

Look up the strerror manpage. (note, though that write() should only return at worst -1 and the error code in errno ).

Formally it is not returning -8, but an error code. These error codes can be found in the manual (eg here: http://linux.die.net/man/2/write ), and you should use these codes to verify if everything went OK.

so, you would use:

 write(..);
if( errno == EFAULT ) { ...}

尝试“ strerror ”和“ errno ”。

You should not print out the return value itself, but strerror(return_value) . You can also look at man errno for error description.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM