Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can either use <code>perror()</code> if you don't mind working within the constraints of its formatting, or you can do the equivalent of:</p> <pre><code>fprintf(stderr, "Failed to open %s (%d: %s)\n", filename, errno, strerror(errno)); </code></pre> <p>You should only ever look at <code>errno</code> after a function has reported failure. It may also be set after a function that succeeds. For example, on Solaris, if the output is not a 'tty', you will often find ENOTTY in <code>errno</code> after a successful print operation - because it checked whether the output file descriptor was a tty and actually it wasn't. But the function succeeded.</p> <p>If there are any more function calls in your code than in the line I showed, it is best to preserve <code>errno</code> early on - because it gets tampered with by other functions (one of the many demerits of a global variable). So, I often write:</p> <pre><code>{ int errnum = errno; ...any other processing... fprintf(stderr, "%s: failed to open %s for reading (%d: %s)\n", prog_name, file_name, errnum, strerror(errnum)); } </code></pre> <p>Well, I write the logical equivalent of that; I have a library that reports errors, so I'd actually write:</p> <pre><code>err_sysrem("failed to open %s for reading", file_name); </code></pre> <p>All the functions in the package start <code>err_</code>. The function <code>err_sysrem()</code> is a remark (does not exit) and includes the system error information - <code>errno</code> and <code>strerror(errno)</code>. If I called <code>err_syserr()</code> instead, it would terminate the program, and <code>err_remark()</code> would not include the information from <code>errno</code>, and neither would <code>err_error()</code>. I find the brevity provided by the functions beneficial. The functions all automatically include the program name at the start of the message; you can configure it to include time stamps or PIDs or other information too. You use <code>err_setarg0(argv[0]);</code> in <code>main()</code> to set the program name; you can, of course, change it at any time.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload