title: How to read files and skip x lines
body: <p>The code I have only prints the first x lines. I really appreciate if you can give me some pointers on what I am missing,</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  int main ( void )
  {
      static const char filename[] = "testfile.txt";
  
      int lineNumbers[] = {1,2};
      size_t numElements = sizeof(lineNumbers)/sizeof(lineNumbers[0]);
      size_t currentIndex = 0;
  
      FILE *file = fopen ( filename, "r" );
      int i =0;
      if ( file != NULL )
      {
          char line [ 328 ]; /* or other suitable maximum line size */
          while ( fgets ( line, sizeof line, file ) != NULL )
          { /* read a line */
              i++;
              if (i  == lineNumbers[currentIndex])
              {
                  fputs( line, stdout ); /* write the line */
                  if (++currentIndex == numElements)
                  {
                      break;
                  }
              }
          }
      fclose ( file );
  }
  </code></pre>