title: Why does this code write input on second line?
body: <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  
  
  void main() {
  
      FILE* fp;
      char line[1024];
      char filename[1024];
      int length;
      int counter=0;
  
      while (1) {
         // fopen(filename, &quot;w&quot;);
          int op = 0;
          printf(&quot;1. new file&quot;);
          scanf(&quot;%d&quot;, &amp;op);
         
          switch (op) {
  
          case -1: break;
          case 1:
              
              printf(&quot;filename&gt;&gt;&quot;);
              scanf(&quot;%s&quot;, filename);
              length = strlen(filename);
              printf(&quot;length = %ld \n&quot;, strlen(filename));
              filename[length] = '.';
              filename[length + 1] = 't';
              filename[length + 2] = 'x';
              filename[length + 3] = 't';
              filename[length + 4] = '\0';
            
              fp=fopen(filename, &quot;w&quot;);
  
              while (fgets(line, sizeof line, stdin) != NULL)
              { 
                  if (line[0] == 'q' || line[0] == 'Q')
                  {
                      printf(&quot;end of line \n&quot;);
                      fclose(fp);
                      break;
                  }
                  else
                      fputs(line, fp);
              }
          }
      }
  }
  </code></pre>
  <p>I made this code to make text file with what I want.
  but the problem is,
  when I run this program, it writes input starting from the second line, not
  the first.</p>
  <p>Why is this happening?
  I want to write input starting from the first line.
  How to change this code to write on the first line?</p>