title: Why does 'fopen' return a NULL pointer?
body: <p>I'm working on a simple file splitter/merger program in the C programming language. The problem is, for some reason <a href="http://en.cppreference.com/w/c/io/fopen" rel="noreferrer">fopen</a> returns NULL, and because of that, my program is crashing at the <a href="http://en.cppreference.com/w/c/io/fwrite" rel="noreferrer">fwrite</a> statement. How do I fix this?</p>
  
  <p>Here is the C file:</p>
  
  <pre><code>int SplitFile(char* filename, char* output, size_t size)
  {
      char current_file_name[256];
      int file_count = 0, i = 0;
      FILE *file = fopen( filename, "rb" );
      printf("split %s into chunks of %d named\n", filename, size);
  
      if (!file)
         return E_BAD_SOURCE;
      else
      {
          output = (char *) malloc(size * sizeof(char));
          if (output == NULL)
              return E_NO_MEMORY;
          else
          {
              int bytes_read = 0;
              FILE *outFile;
              do
              {
                  bytes_read = fread(output, sizeof(char), size, file );
                  sprintf(current_file_name, "%s%04lu\n", "part", file_count++);
                  outFile = fopen (current_file_name, "wb" );  // THIS RETURNS NULL
                  fwrite(output, sizeof(char), bytes_read, outFile); //CRASHES ON THIS LINE
              }
              while ( bytes_read &gt; 0 )
                  ;
  
              //fclose(outFile);
          }
      }
      fclose(file);
      printf("...\n");
      return 0;
  }
  </code></pre>