1

Is there a way to determine the amount of data transfered to/from a device. I'm not asking about the block or sector size. I want to know the amount transferred by a read/write operation.

For example, if I want to transfer 1 block of 0x200 bytes:

blocks=fread((void *)&Block, 0x200, 1, FILE *fp);

what happens if I reach end of file and only a partial block is read, and how do I determine the actual byte count.

I realize I could do the opposite as in:

bytes=fread((void *)&Block, 1, 0x200, FILE *fp);

but what if I want to read multiple blocks???

Is there a kernel function such as ioctl(dev, FNUM) that will return the actual byte count?

Much thanks, CB

0

As you have realized, you can use a size parameter of 1 byte, and number of items as 0x200.

bytes = fread((void*)&Block, 1, 0x200, fp);

Then fread will return the number of items (number of bytes) actually read.

Typically fread would be sited in a loop to read all blocks. Alternatively (depending on your actual requirements) fstat or stat system calls can be used to obtain the size of the file before reading it.

From the fread man page, the short object count referred to means for example if there are 0x201 bytes in the file being read, the first read will return 0x200, the second read will return 1.

RETURN VALUES

 The functions fread() and fwrite() advance the file position indicator for
 the stream by the number of bytes read or written.  They return the number
 of objects read or written.  If an error occurs, or the end-of-file is
 reached, the return value is a short object count (or zero).
  • Hmmm. I always figured fread()/fwrite() were more for block oriented transfers, hence the 'size' parameter. I guess the alternative would be to actually track transfers manually, and use a 'size' of 1 as suggested. Seems like reinventing something though.in – Cpmb Aug 29 '14 at 1:38
  • My only concern with fstat() or stat() would be if accessing a newly created rw file the file system may not report a size until the file is closed. Thanks for the help. Will check out those functions. – Cpmb Aug 29 '14 at 1:39
  • Without knowing how much control you have over the file - the easiest thing to do is close the file and re-open it if necessary. – suspectus Aug 29 '14 at 8:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.