title: Program hangs after opening file with open and fdopen
body: <p>I'm using <code>open()</code> with <code>O_RDWR</code> and then using the descriptor in <code>fdopen()</code> with <code>"r+"</code>. I check that the file exists first with <code>access()</code>, and check for <code>open()</code> returning -1 then check that the <code>FILE *fp</code> set from <code>fdopen()</code> isn't <code>NULL</code>.</p>
  
  <p>Now whenever I use any function such as <code>fgets()</code> or <code>fgetc()</code> with <code>fp</code> the program hangs in my terminal. I get no compiler warnings. Any ideas?</p>
  
  <p><img src="https://i.stack.imgur.com/oZIiG.gif" alt=""></p>
  
  <pre class="lang-c prettyprint-override"><code>#include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;unistd.h&gt;
  
  int main(int argc, char **argv) {
      char buffer[512];
      int file_descriptor;
      FILE *file;
  
      if (argc != 2) {
          puts("ERROR: Missing argument");
          exit(0);
      }
      else if (access(argv[1], F_OK) == -1) { // Check if file exists
          puts("ERROR: File does not exist");
          exit(0);
      } else if (file_descriptor = open(argv[1], O_RDWR) == -1) { // Check for open() failure
          puts("ERROR: Unable to open file using open()");
          exit(0);
      }
  
      file = fdopen(file_descriptor, "r+"); // Use descriptor
  
      if (file == NULL) {
          puts("ERROR: Unable to open file using fdopen()");
          fclose(file);
          exit(0);
      }
  
      // fscanf(file, "%c", stdout); // Hangs
      // fgets(buffer, sizeof(buffer), file); // Hangs
      printf("%c", fgetc(file)); // Hangs
  
      fclose(file);
  
      return 0;
  }
  </code></pre>