Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>read</code> 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.</p> <p>For example, if there's only 22 bytes left in the file (or the pipe), it will give you those bytes.</p> <p>From the <a href="http://linux.die.net/man/2/read" rel="nofollow noreferrer">Linux man page</a> (since <code>read</code> is a POSIX thing rather than a C++ thing):</p> <blockquote> <p>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.</p> </blockquote> <p>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 <code>read</code> returns 1, then only use that one character. If you follow the rules and characters seem to disappear, <em>then</em> you can come back and complain about a buggy <code>read</code> implementation (I shouldn't need to point out that this is incredibly unlikely).</p> <p>Try the following program:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; int main (void) { char buff[255]; int i; while ((i = read (0, buff, 255)) &gt; 0) { printf ("%3d: [%*.*s]\n", i, i, i, buff); } return 0; } </code></pre> <p>with:</p> <pre><code>echo hello there | ./tstprg ; (echo hello ; sleep 1 ; echo there) | ./tstprg </code></pre> <p>and see what you get. My output is:</p> <pre><code> 12: [hello there ] 6: [hello ] 6: [there ] </code></pre> <p>as I expect.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload