title: How to use fgets to read a file line by line
body: <p>I'm new at programming so there are some basics and maybe common sense that I don't know. I have a question about how to use fgets right. Based on the explanation of fgets, it seems that fgets should stop whenever it reads n-1 characters, hit the EOF or hit a newline character. 
  For example, I create a text file like below:</p>
  
  <pre><code>red  100
  yellow  400
  blue  300
  green 500
  purple 1000
  ...
  </code></pre>
  
  <p>The color and the integer is separated by a tab. When I create this text file, I need to hit enter at the end of each line to start a new line. In this case, hitting enter equals to add a newline character, '\n', is that right? </p>
  
  <p>If it is right that there is a '\n' at the end of each line, I run the fgets code as below:</p>
  
  <pre><code>fgets(string, 100, fp);
  </code></pre>
  
  <p>Since the characters contain in each line is much less than 100, the fgets should hit the newline character before reach the maxlength limit and it should stop and return a NULL. Is that correct?</p>
  
  <p>If my understanding above are not right, there is no '\n' at the end of each line, or fgets does not stop at the end of each line, what is the number of maxlength (i.e., the N in the fgets(string, N, stream) function) should I pick to make sure that the file input properly due to my ultimate goal is to parsing each line and store each line into a structure. By the way, there are 100 lines in the file.</p>