title: C stdin and CTRL+C copies previous line
body: <p>I am trying to read from <code>stdin</code> with <code>fgets</code> which is an alternate way to read apart from a file. The thought is that I should be able to write as many lines as I want and stop reading from stdin with <code>CTRL+C is entered or any other bash terminating commands</code>.</p>
  
  <p>However the issue is that if I write a line, for instance:</p>
  
  <pre><code>mr:x:1171:1101:Mikael Rännar:/Home/staff/mr:/usr/local/bin/tcsh
  </code></pre>
  
  <p>and then I press <code>ENTER</code> to submit it and thereafter <code>CTRL+C</code> then it counts the line I'm terminating at the same as previous one. Here are a few examples:</p>
  
  <pre><code>mr:x:1171:1101:Mikael Rännar:/Home/staff/mr:/usr/local/bin/tcsh
  &lt;BLANKLINE&gt;
  *I PRESSED CTRL+C HERE*
  Output:
  Line 2: Encountered a &lt;BLANKLINE&gt;
  Line 3: Encountered a &lt;BLANKLINE&gt;
  1171:mr
  </code></pre>
  
  <p>Example 2:</p>
  
  <pre><code>mr:x:1171:1101:Mikael Rännar:/Home/staff/mr:/usr/local/bin/tcsh 
  *I PRESSED CTRL+C HERE*
  Output:
  1171:mr
  1171:mr
  </code></pre>
  
  <p>So my question is, how do I stop the program from copying the previous line when a terminating command is sent (CTRL+C)?</p>
  
  <p>EDIT: This is my code I use to read from the stream</p>
  
  <pre><code>void readFile(FILE *fp, list *userList) {
      char line[1025];
      int lineNumber = 1;
      while (!feof(fp)) {
          user *newUser = malloc(sizeof(user));
          initializeStruct(newUser);
          fgets(line, 1025, fp);
          char *p = memchr(line, '\0', 1024);
  
          if (p == NULL) {
              fprintf(stderr, "Line %d: Line is too long!\n", lineNumber);
          } else {
              newUser-&gt;lineNumber = lineNumber;
              parseLine(line, newUser);
              list_insert(userList, newUser);
              lineNumber++;
          }
      }
  }
  </code></pre>
  
  <p>When <code>CTRL+D</code> is pressed in bash, the fgets command uses the previous line entered.</p>
  
  <p>Thanks</p>