title: dynamic buffer size for reading input
body: <p>I am trying to create a program that will read line by line from stdin, search that line for the start and end of a given word and output all the matching words. Here is the code:</p>
  
  <pre><code>int main()
  {
      char buffer[100];
      char **words = NULL;
      int word_count = 0;
  
      while (fgets(buffer, sizeof(buffer), stdin) != NULL) {
          int length = strlen(buffer);
          if (buffer[length - 1] == '\n') {
              word_count = count_words(buffer, FIRSTCHAR);
              if (word_count &gt; 0) {
                  words = get_words(buffer, FIRSTCHAR, LASTCHAR);
                  for (int i = 0; i &lt; word_count; ++i) {
                      printf("%s\n", words[i]);
                      free(words[i]);
                  }
                  free(words);
              }
          }
      }
      return 0;
  }
  </code></pre>
  
  <p>I got the basic functionality working, but I am relying on fgets() with a fixed buffer size.
  What I would like is to dynamically allocate a memory buffer with a size based on the length of each line.</p>
  
  <p>I can only see one way of going about solving it, which is to iterate over input with <strong>fgetc</strong> and increment a counter until end of line and use that counter in place of <em>sizeof(buffer)</em>, but I don't know how I would get fgetc to read the correct relevant line.</p>
  
  <p>Is there any smart way of solving this?</p>