tags:

views:

348

answers:

4

With fwrite returning the number of successful elements written to the file, by saying:

if (!(fwrite(...))) {
    fprintf(stderr, "Failure");
    //perror(???)  I sometimes see code that says perror here and I don't know 
    //exactly what this does.
}

Does this check for successful writing to the file? Are there other things to worry about?

Thanks.

+3  A: 

In short, not quite. fwrite returns the number of elements successfully written; you need to check this against the number of elements you intended to write i.e. those you passed in argument to fwrite.

What you've done checks that some elements have been written.

Here's a reference for perror.

Interprets the value of the global variable errno into a string and prints that string to stderr (standard error output stream, usually the screen), optionaly preceding it with the custom message specified in str. errno is an integral variable whose value describes the last error produced by a call to a library function. The error strings produced by perror depend on the developing platform and compiler. If the parameter str is not a null pointer, str is printed followed by a colon (:) and a space. Then, whether str was a null pointer or not, the generated error description is printed followed by a newline character ('\n'). perror should be called right after the error was produced, otherwise it can be overwritten in calls to other functions.

Ninefingers
A: 

Your code might not check for errors properly. Use

if (fwrite(ptr, size, num, f) != num) {
    // An error occurred, handle it somehow
}
Tronic
Depending if you are handling errno == (EAGAIN | EINTR) gracefully.
mctylr
A: 

From the Linux man page of fwrite

fread() and fwrite() return the number of items successfully read or written (i.e., not the number of characters). If an error occurs, or the end-of-file is reached, the return value is a short item count (or zero).

so you need to compare with what is expected return value. In many cases you may need to check for errno equal to EAGAIN or EINTR, in which case you normally want to retry the write request, while in other cases you want to handle short writes gracefully.

mctylr
A: 
STRERROR(3)            FreeBSD Library Functions Manual            STRERROR(3)

NAME
     perror, strerror, strerror_r, sys_errlist, sys_nerr — system error mes‐
     sages

LIBRARY
     Standard C Library (libc, -lc)

SYNOPSIS
     #include <stdio.h>

     void
     perror(const char *string);

     ...

DESCRIPTION
     ...

     The perror() function finds the error message corresponding to the cur‐
     rent value of the global variable errno (intro(2)) and writes it, fol‐
     lowed by a newline, to the standard error file descriptor.  If the argu‐
     ment string is non‐NULL and does not point to the null character, this
     string is prepended to the message string and separated from it by a
     colon and space (“: ”); otherwise, only the error message string is
     printed.

...

STANDARDS
     The perror() and strerror() functions conform to ISO/IEC 9899:1999
     (“ISO C99”).  ...
just somebody