title: Reading wrong input from file
body: <p>I am trying to write a program which should read a line and store its contents in an array, so it needs to read line by line and also read different characters in a line. For example my input is</p>
  
  <pre><code>4 6
  0 1 4
  0 2 4
  2 3 5
  3 4 5
  </code></pre>
  
  <p>First two characters will determine something else and I need to read a line so I can write 0 1 4 in an array and 0 2 4 in another array.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;iostream&gt;
  #include &lt;list&gt;
  #include &lt;iterator&gt;
  
  #define BUFFER_SIZE 50
  
  int main()
  {       
  using namespace std;
  
  int studentCount, courseCount;
  FILE *iPtr;
  iPtr = fopen("input.txt", "r");
  if(iPtr == NULL){ printf("Input file cannot be opened!\n"); return 0; }
  
  fseek(iPtr, 0, SEEK_SET);
  fscanf(iPtr, "%d", &amp;studentCount);
  fscanf(iPtr, "%d", &amp;courseCount);
  
  list &lt;int&gt; S[studentCount]; // an array of linked lists which will store the courses
  char buffer[BUFFER_SIZE];
  char temp[BUFFER_SIZE];
  int data;
  int x=0, counter=0; // x traces the buffer
  
  fgets(buffer, BUFFER_SIZE, iPtr);
  while( buffer[x] != '\0')
  {
     if( isspace(buffer[x]) ) counter++;
     x++;
  }
  printf("%d\n", counter);
  
  fflush(stdin);
  getchar();
  fclose(iPtr);
  return 0;
  }
  </code></pre>
  
  <p>When I debug and follow the values of buffer[x] I see that it always have the value "10 \n" when x=0 and then "0 \0" when x=1. How can I fix this, or is there a better method for reading line by line? I also need the number of data in a line so using fgets or getline is not enough by itself.</p>