How to check number of characters successfuly written?

How to check number of characters successfuly written?

 
size = fwrite(pBlock, 14+40, 1, fp);


I am not sure what means the 3rd argument and how it should be used. Does that mean that
 
size = fwrite(pBlock, 14+40, 2, fp);

would write 2x54 bytes to file? Is there some possibility how to check if all characters were successfully written? What if disk runs out of space or there is some problem? Will the return value be 0 instead 1?
How to check number of characters successfuly written?


the return value of fwrite() tells you exactly this.
I don't think so. It always returns 1
size = fwrite(pBlock, 14+40, 1, fp);
Also I read that the number of elements is not number of characters!
then you've written 1 byte.

Also I read that the number of elements is not number of characters!


you'd have to use sizeof() if you were passing in a char array as your 3rd param.

Read here on error codes as well:
http://stackoverflow.com/questions/2329842/checking-for-success-of-fwrite-in-c-perror
particularly:
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.
Last edited on
OK, thanks. Now it's clear.
the return value of fwrite() tells you exactly this.

No it doesn't. The second and third arguments to fwrite are the size of an "item" and the number of items respectively. The return value is the number of items written, not the number of bytes. So if you say
size = fwrite(pBlock, 14+40, 1, fp);
and the return value is 1, then the 1 item was successfully written. If size is less than the number of items, then errno will tell you what error occurred.

I used this:
1
2
3
4
5
  size = fwrite(pBlock, sizeof(unsigned char), 14+40, fp);
  if (size < 14+40) {
    fclose(fp);
    return W_HEADER_ERROR;
  }


It would be probably better to use fixed size in this case coz IDK what is size of unsigned char on other machines than mine x86
Last edited on
Sorry OP. my apologies for the incorrect information about the return value. although my quote from that link does indeed say the number of elements.
the 3rd parameter is the number of elements you want to write, the second is the size of each individual element. Supposing you have a struct of size 256 bytes, and you have an array of 100 such structs you want to write, then your second argument would be 256, and the 3rd would be 100; if successful the return value would be 100, and the total number of bytes written will be 100 * 256

Usually (quoting @mutexe) you'd use sizeof (your struct), instead of just 256.

If the call fails you would either get zero returned or the number of items successfully written. So if the disk was full after 45 * 100 bytes have been written (and miraculously your program/system is still running), you will get 45 returned, not 4500.

From the man page:

size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);

On success, fwrite() returns the number of items read or written. This number equals the number of bytes transferred only when size is 1. If an error occurs, or the end of the file is reached, the return value is a short item count (or zero).

Last edited on
Topic archived. No new replies allowed.