title: 'fopen' in C can't open existing file in current directoy on Unix
body: <p>I am using <a href="http://en.cppreference.com/w/c/io/fopen" rel="nofollow noreferrer">fopen</a>(3) in C to read file and process it. The file is present in current working directory where the binary exists, but I am unable to read the file (Linux environment / <a href="http://en.wikipedia.org/wiki/Cygwin" rel="nofollow noreferrer">Cygwin</a> environment).</p>
  
  <p>Here is the sample code:</p>
  
  <h1>C code:</h1>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  #include&lt;string.h&gt;
  
  FILE *inFile;
  static char fileName[255];
  
  int process_file(FILE *inFile)
  {
      char ch;
      inFile = fopen(fileName,"r");
      if (inFile == NULL)
      {
          perror(fileName);
          exit(1);
      }
      else
      {
         // Process file
      }
      fclose(inFile);
      return 0;
  }
  
  int main(int argc, char *argv[])
  {
      printf("Enter filename to process \n");
      scanf("%s", fileName);
      process_file(inFile);
      getchar();
      return 0;
  }
  </code></pre>
  
  <p>I have file permissions set to 777 in the current directory. The resulting binary as well as my source code reside in this directory where the input file exits. Why is the file not opened?</p>
  
  <h1>Update :</h1>
  
  <pre><code>This question was written in few years back and this code could be improved a lot.
  1. The process file should accept char * or char array instead of file pointer
  2. unused variables can be removed
  3. unused libraries or include files can be removed
  4. Can make use of argv to accept filename with path from cmdline
  5. return instead of exit in process_file and also proper return code instead of returning 0 from process_file.
  </code></pre>