0

I'm fairly new to c so bear with me.

  1. How do I go about finding out the number of chars read/write reads?

  2. Can I be more specific and designate the # of chars read/write reads in an argument? If so, how?

2 Answers2

2

From man(2) read:

If successful, the number of bytes actually read is returned

From man(2) write:

Upon successful completion the number of bytes which were written is returned

Now concerning:

Can I be more specific and designate the # of chars read/write reads in an argument? If so, how?

AFAIK no, but there might be some device/kernel specific ways using for example ioctl(2)

Drax
  • 12,682
  • 7
  • 45
  • 85
0

C and C++ has different IO libraries. I guess you are coding in C.

fprintf(3) returns (when successful) the number of printed characters.

scanf(3) returns the number of successfully read items, but also accept the %n specifier:

   n      Nothing is expected; instead, the number of characters
          consumed thus far from the input is stored through the next
          pointer, which must be a pointer to int. 

You could also do IO line by line... (getline, snprintf, sscanf, fputs ....)

for Linux and Posix

If you call directly the read(2) or write(2) functions (i.e. syscalls) they return the number of input or output bytes on success.

And you could use the lseek(2) syscall, or the ftell(3) <stdio.h> function, to query the current file offset (which has no meaning so would fail on non-seekable files like pipes, sockets, FIFOs, ...).

See also FIONREAD

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547