title: Output blocks of text in C
body: <p>I have a large character array <code>data</code> and I am wanting to output it to the terminal 30 lines at a time. So I decided to count the characters between each set of 30 new line characters, so I could offset <code>strncpy</code> by that much. However when I go to print it out, I get no output. There must be something small but I can't see what's wrong.</p>
  
  <pre><code>char* data;
  int blocks[100], block_sizes[100];
  int traversed, block_idx, nl_chars, i, len, chars;
  char* block;
  
  // Determine output blocks
  nl_chars = 0;
  block_idx = 1;
  blocks[0] = 0;
  chars = 0;
  len = strlen(data);
  for (i = 0; i &lt; len; i++) {
      chars++;
      if (data[i] == '\n') {
          nl_chars++;
      }
  
      if (data[i] == '\0' || nl_chars == 30) {
          blocks[block_idx] = i;
          block_sizes[block_idx - 1] = chars;
          block_idx++;
          nl_chars = 0;
          chars = 0;
      }
  }
  
  // Output blocks to stdout
  traversed = 0;
  while (traversed &lt; block_idx) {
      block = malloc(block_sizes[traversed] * sizeof(char));
      if (block == NULL) {
          error("[-]Unable to allocate required memory");
      }
      strncpy(block, data + blocks[traversed], block_sizes[traversed]);
      printf("%s", block);
      free(block);
  
      while (1) {
          memset(input, 0, BUFFER_SIZE);
          fgets(input, sizeof(input), stdin);
          fflush(stdin);
          if (input[0] == '\n' || input[0] == '\r') {
              break;
          }
      }
  
      traversed++;
  }
  </code></pre>