title: Does fopen return NULL pointer if file is already open?
body: <p>I was assuming that <code>fopen</code> returns <code>NULL</code> pointer if file is already open. But it looks <code>fopen</code> does not return <code>NULL</code> in case file is already open in <code>&quot;w&quot;</code> mode. Below is the code that I used to try this and I do not get any errors. I tried with mingw32 as well as TDM-GCC-64 compilers. If I am not mistaken, C++ gives error if file is already open.</p>
  <pre><code>#include&lt;stdio.h&gt;
  
  int main()
  {
      FILE *fp1, *fp2;
      
      fp1 = fopen(&quot;file1.txt&quot;, &quot;w&quot;);
      fp2 = fopen(&quot;file1.txt&quot;, &quot;w&quot;);
      
      if(fp2 == NULL)
      {
          printf(&quot;Error in opening file\n&quot;);
          return(0);
      }
      
      // Edit: added following code to check the behavior if write operation
      // is performed simultaneously
      
      fputc('A', fp1);
      fputc('M', fp1);
      fputc('S', fp1);
      fputc('B', fp2);
      
      fclose(fp1);
      fclose(fp2);
      
      return 0;
  }
  </code></pre>
  <p>Edit: Added extra code to write some data to both <code>fp1</code> and <code>fp2</code> and see the behavior. If executed, <code>file1.txt</code> contains data <code>BMS</code> and seems to be correct behavior and <code>fp1</code> and <code>fp2</code> move independently as expected. First <code>AMS</code> is written using <code>fp1</code> and then <code>A</code> is replaced by <code>B</code> using <code>fp2</code> and final output is <code>BMS</code>.</p>