Number of bytes read from the channel

When reading from a pipe in Linux (C, fread / similar), when EOF is reached, how can one know how many bytes have been read? If I read blocks at a time, fread () only returns the number of complete blocks, and I cannot read one byte at a time because it is too slow. Of course ftell () returns -1.

+1


a source to share


2 answers


You can do this with by fread()

setting the parameter size

to 1 and set nmembers

to any size. Then the number of "members" is the number of bytes, and you can still have a decent buffer size:

char     buf[8192];
size_t   n;
n = fread(buf, 1, sizeof buf, f);

      



instead

char     buf[8192];
size_t   n;
n = fread(buf, sizeof buf, 1, f);

      

+6


a source


read()

returns the number of bytes read (when nothing happens).



+1


a source







All Articles