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.
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.