title: Making getline() in C
body: <p>I wanted to make my own getline() like function in C and this is what I came up with:</p>
  
  <pre><code>void getstring(char *string)
  { 
      fgets(string,STRING_LENGTH,stdin);
      string[strlen(string)-1]='\0';
  }
  </code></pre>
  
  <p>It's good enough if number of typed characters doesn't exceed STRING_LENGHT (in this case 100) but once it does, everything above it is stays in buffer and jumps on next read string.</p>
  
  <p>I've already tried to flush buffer by using following procedure: </p>
  
  <pre><code>void flush_buffer()
  {
      char c;
      while((c = getchar()) != '\n' &amp;&amp; c != EOF)
          /* discard */ ;
  }
  </code></pre>
  
  <p>It does its job in described case, but once the string doesn't exceed STRING_LENGHT I need to type anything before moving on.</p>
  
  <p>Is there any way to read string such that I know whether STRING_LENGHT is exceeded or not so I can condition the flushing?</p>
  
  <p>If there's a better way to make getline() like function, it would be even better.</p>