title: C - reading from stdin BUFSIZE characters at a time
body: <p>I'm writing a small sockets program (GNU libc). I have a loop which asks user for input (e.g. "MSG> "). When the user presses enter the message is sent (currently to a server on localhost).</p>
  
  <p>Anyway, I want to read from stdin into a char buffer[256]. I'm currently using fgets() which doesn't do what I want. I'm not sure how write the code such that I ask the user and then get data 256 -1 bytes at a time so that I can send a c-string of 1000 bytes via several strings of 256 bytes.</p>
  
  <p>EDIT: Add code</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  #define BUFSIZE 256
  
  int main(int argc, char *argv[])
  {
    char msg[BUFSIZE];
    size_t msgLen;
  
    msgLen = strlen(fgets(msg, BUFSIZE, stdin));
    puts(msg);
  
    // This simply checks whether we managed to fill the buffer and tries to get
    // more input
    while (msgLen == (BUFSIZE - 1))
      {
        memset (msg, '\0', BUFSIZE);
        fread(msg, BUFSIZE, 1, stdin);
        msg[BUFSIZE - 1] = '\0';
        msgLen = strlen(msg);
        puts(msg);
        if (msgLen &lt; (BUFSIZE - 1))
      break;
      }
  
    return 0;
  }
  </code></pre>