It seems as if read(0, buffer, 255) always returns 1 when reading. In my program I direct input through the pipe into this program, but I noticed that it always returned a 1. Why is this? Is there a better way of doing this? At the same time, it seems to fill up the buffer properly, past the 1 char.
Thank you in advance!
readis allowed to return less characters than you asked for, so you should code for that eventuality rather than trying to find a way to avoid it.For example, if there’s only 22 bytes left in the file (or the pipe), it will give you those bytes.
From the Linux man page (since
readis a POSIX thing rather than a C++ thing):If the rest of your buffer seems to be populated correctly, I can assure you that it’s entirely by accident and you shouldn’t rely on that. If
readreturns 1, then only use that one character. If you follow the rules and characters seem to disappear, then you can come back and complain about a buggyreadimplementation (I shouldn’t need to point out that this is incredibly unlikely).Try the following program:
with:
and see what you get. My output is:
as I expect.