title: How to embed a file into an executable?
body: <p>I have a small demo executable wrote in C++ that depends only on one 5kb PNG image being loaded before it can run, which is used for a pixel text I made.  Because of this one file, I would need to give out a ZIP archive instead of just one executable file, which creates enough friction between download and 'play' that I believe  would dissuade some from trying it out.</p>
  
  <p>My question is, <em>is there anyway to embed the PNG file (and any other file really) into the Executable or source code so that it is a single file, and the executable can use it?</em></p>
  
  <p>I have the ability to parse the PNG as a byte stream, so it does not need converted to pixel data.</p>
  
  <p>Thanks in advance!  (Other questions with a similar title to this exist, but they and their answers seem to get into more specific issues and weren't very helpful)</p>
  
  <p><strong>edit</strong>:The compiler is Visual C++ 2010 and this is on Windows (though I would want to avoid windows specific utilities for this)</p>
  
  <p><strong>edit2</strong>:  Alf's answer seemed like the most portable method, so I quickly wrote a function to parse the PNG file into a TXT or header file that could be read as a <code>unsigned char</code> array.  It appears to be identical in this form to the PNG file itself, but my png loader won't accept the array.  When loading it from memory, the PNG parser takes a <code>(void * buffer,  size_t length)</code> if it matters.</p>
  
  <p>The code if you wanted to see, but I'll still accept other answers if you think they're better than this method:</p>
  
  <pre><code>void compileImagePNGtoBinary(char * filename, char * output){
  
      FILE * file = fopen(filename, "rb");
      FILE * out = fopen(output, "w");
  
      unsigned char buffer[32];
      size_t count;
      fprintf(out, "#pragma once \n\n static unsigned char TEXT_PNG_BYTES[] = { ");
      while(!feof(file)){
              count = fread(buffer, 1, 32, file);
  
              for(int n = 0; n &lt; count; ++n){
                      fprintf(out, "0x%02X, ", buffer[n]);
              };
      };
      fprintf(out, "};");
      fclose(file);
      fclose(out);
  
  };
  </code></pre>
  
  <p><strong>Final Edit</strong>: <a href="http://www.imagemagick.org/script/command-line-processing.php">ImageMagick</a> which Alf also mentioned did exactly what I needed of it, thanks!</p>