title: Why is rewind() not working as expected in this simple program?
body: <p>Why is the below program not printing the first character of the newly created text file (<strong>"E"</strong>) as expected? It's a simple program and I tried to look at the issue from all aspects but couldn't find the reason. The text file is being created on my D drive with the content "<strong>EFGHI</strong>", but for some reason <strong>"E"</strong> is not being read even if I rewind and read using <code>getc()</code> and the output is <strong>-1</strong>.</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  
  int main()
  {
      int x;
      FILE *fp;
      fp=fopen("F:\\demo.txt","w");
      if(fp==NULL)
          puts("Write error");
      fputs("EFGHI",fp);
      rewind(fp);
  
      x=getc(fp);
      printf("%d",x);
      fclose(fp);
  }
  </code></pre>
  
  <p><strong>UPDATED:</strong></p>
  
  <pre><code>    #include&lt;stdio.h&gt;
      #include&lt;stdlib.h&gt;
  
      int main()
      {
          int x;
          FILE *fp;
          fp=fopen("F:\\demo.txt","w+");
          if(fp==NULL)
          {
              puts("Write error");
              exit(EXIT_SUCCESS);
          }
          fputs("EFGHI",fp);
          rewind(fp);
  
          while(!feof(fp))
          {
              x=getc(fp);
              printf("%d\n",x);
          }
          fclose(fp);
       }
  </code></pre>