tags:

views:

47

answers:

1

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!

A: 

read is 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 read is a POSIX thing rather than a C++ thing):

It is not an error if this number is smaller than the number of bytes requested; this may happen for example because fewer bytes are actually available right now (maybe because we were close to end-of-file, or because we are reading from a pipe, or from a terminal), or because read() was interrupted by a signal.

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 read returns 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 buggy read implementation (I shouldn't need to point out that this is incredibly unlikely).

Try the following program:

#include <stdio.h>
#include <stdlib.h>

int main (void) {
    char buff[255];
    int i;

    while ((i = read (0, buff, 255)) > 0) {
        printf ("%3d: [%*.*s]\n", i, i, i, buff);
    }

    return 0;
}

with:

echo hello there | ./tstprg ; (echo hello ; sleep 1 ; echo there) | ./tstprg

and see what you get. My output is:

 12: [hello there
]
  6: [hello
]
  6: [there
]

as I expect.

paxdiablo
The thing is that I the rest of the buffer is populated properly, no matter how many characters I send it with the [echo "blalala" | project], it always tells me I only am reading 1 character, but it fills up the rest of it correctly! grrr
Cenoc
@Cenoc, you say it _seems_ to be populating correctly but what happens on the next read? Is the first char in that the next character or the character 255 forward? It may be that read copies a big chunk from the internal buffers but still only advances the internal buffer pointer by 1. Bottom line, if read says 1, that's what you use, regardless of what's in the buffer. Now, if you use the 1 and start _losing_ characters because of that, _then_ we need further investigation.
paxdiablo
In other words, if you pipe in "123456789", and do two reads (the first returning 1), does the second read return "2" as the first character or something else (including a return code of 0 for EOF)?
paxdiablo
Oddly enough, I cleaned it, and it started working according to what I would expect....
Cenoc
@Cenoc: What do you mean you cleaned it? Does it indeed return only one character per `read` and fill the buffer as a "bonus," or did you do something else?
Potatoswatter