2

So I was trying to write something to stdout from my p->payload which is supposed to be a pointer to a data. And block_size is 256.

My problem is my stdout would always be exactly the same if I switch p->block_size and sizeof(char). Can someone tell my why? Thanks.

fwrite(p->payload, p->block_size,sizeof(char),stdout);
Pig
  • 2,002
  • 5
  • 26
  • 42
  • 1
    See also ["How does fread really work?"](http://stackoverflow.com/q/8589425/827263) – Keith Thompson Feb 14 '14 at 02:46
  • Note that with the arguments `p->block_size` and `sizeof(char)`, unless `p->block_size` is `1`, reversing the order of the arguments changes the value returned by `fwrite()`. It reports the number of items of the given size that were written, so in one case you'll get `p->block_size` returned on success (and 0 on error), and in the other you'll get `1` returned on success (and 0 on error). – Jonathan Leffler Feb 14 '14 at 02:56

3 Answers3

3

The difference is the value that it returns.

fwrite returns the number of data elements written.

If you execute

count = fwrite(ptr, 1, N, stdout);

then count will contain the exact number of bytes successfully written. If you do:

count = fwrite(ptr, N, 1, stdout);

then count will contain 1 if the entire write succeeded, or 0 if any of it failed.

The distinction may be useful if you need to distinguish between partial success and complete failure. If a partial write is no better than not writing anything at all, you can use the second form.

(There's not likely to be any significant difference in performance.)

See also this similar question about fread.

Community
  • 1
  • 1
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
2

In general, just assume size * items bytes will be written.

This seems to be an attempt to mirror the fread API, which also has size and bytes parameters. For fread, this is more useful, as (for instance) you can ask it to read one item of 1000 bytes--meaning, "I want exactly 1000 bytes"--or you can ask it to read 1000 items of one byte apiece--meaning "I'll take up to 1000 bytes; less data than that is OK." Obviously, lots of useful combinations can be used here if your data being read is fixed-size.

In the case of fwrite, though, the only time you'd expect less than size * items bytes to be written would be in an error condition. In the case of "disk full" errors, you could hypothetically choose size and items such that you write as many complete items as possible, but never write a half-item; however, I've never seen anyone actually write code that works this way and doesn't just fail on disk full.

StilesCrisis
  • 15,972
  • 4
  • 39
  • 62
0

According to fwrite(3):

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

The function fwrite() writes nmemb elements of data, each size bytes long, to the stream pointed to by stream, obtaining them from the location given by ptr.

This means fwrite() will write 'size * nmemb' bytes from ptr to stream, because 'size * nmemb' equals 'nmemb * size', so you have the same number of bytes written to stream after switched size and nmemb.

Lee Duhem
  • 14,695
  • 3
  • 29
  • 47