title: How to display 3 lines from a text file in char arrays using strtok in C
body: <p>I have a homework and i can't really find where the problem with the code is. The main problem is to read 3 lines from a text file and use them to build a binary tree. The text file has these lines:</p>
  
  <pre><code>7
  2 4 0 0 7 0 0
  3 5 6 0 0 0 0
  </code></pre>
  
  <p><br /></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  int main(){
    const char* p;
    const char* v1;
    const char* v2;
    char buf[100];
    FILE *fptr = fopen("sd.in", "r");
    if (fptr == NULL) {
      printf("Failed to open file\n");
      return -1;
    }
    if(fgets(buf,100,fptr)!=NULL)
      p=strtok(buf,"\n");
    printf("%s\n", p);
    while((p = strtok(NULL,"\n"))!=NULL)
    {  
      printf("%s\n", p);
    }        
  
    fclose(fptr);
    return 0;
  }
  </code></pre>
  
  <p>This is my code so far. When i compile it, it only shows the first line with number 7. How could i display all the lines? Thank you very much!</p>
  
  <p><strong><em>UPDATE</em></strong> of the code. Right now i can display the first and the second line but without number 2. I want to store the second line in v1 and the third line in v2.</p>
  
  <pre><code>        if(fgets(buf,100,fptr)!=NULL)
           p=strtok(buf,"\n");
           printf("%s\n", p);
         if((p = strtok(buf,"\n"))!=NULL &amp;&amp; fgets(buf,100,fptr)!=NULL)
         v1 = strtok(NULL,"\n");
         printf("%s\n ",v1);
  </code></pre>