title: Determining the extension type of an image file using binary
body: <p>I am trying to write a function that determine if a file in a directory is of a gif/bmp/png/jpg extension. Right now I think I have correctly written my code all the way up to listing the files in the directory and opening them in binary mode. </p>
  
  <p>Now, I am struggling with figuring out how to determine what extension the image is. Right now i am just focusing on writing my "bool isGif();" function... To determine if a file is a .gif extension using binary, the first 6 bytes of the file will contain either GIF87a or GIF89a. So, to do this I would read the first six bytes of the file into an array, and then compare those to arrays that contain "GIF87a" or "GIF89a", correct?</p>
  
  <p>Below is my attempt at coding this up. It gives me 2 warning, but no errors and it runs in the program fine, but it never outputs a message that directory contains a gif, and I know it does, because I put it in there...</p>
  
  <pre><code>getDir();
  
  ifstream fin;
  
  _finddata_t a_file;
  intptr_t dir_handle;
  
  dir_handle = _findfirst("*.*", &amp;a_file);
  
  //if (dir_handle == -1)
  //{
      //return;
  //}
  
  while (_findnext(dir_handle, &amp;a_file) == 0);
  {
      fin.open(a_file.name, ios::in | ios::binary);
  
      if (!fin)
      {
          cout &lt;&lt; endl &lt;&lt; "Could not open the file."
              &lt;&lt; " Attempting to open the next file." &lt;&lt; endl;
          return false;
      }
      else
      {
          cout &lt;&lt; "Files opened successfully."
              &lt;&lt; " Processing through the directory." &lt;&lt; endl;
  
  
              ifstream fl(a_file.name);
              fl.seekg(0, ios::end);
              size_t len = fl.tellg();
              char *ret = new char[len];
              fl.seekg(0, ios::beg);
              fl.read(ret, len);
              fl.close();
  
              char arr1[6] = { 'G', 'I', 'F', 8, 7, 'a' };
              char arr2[6] = { 'G', 'I', 'F', 8, 9, 'a' };
  
              if (ret == arr1 || arr2 )
              {
                  cout &lt;&lt; a_file.name &lt;&lt; " has a .gif extension" &lt;&lt; endl;
                  return true;
              }
  
  
      }
  }
  </code></pre>
  
  <p>Okay, I think I am close on this now... This is the updated/changed snippet of code important to this problem... I am just trying to use a for loop to read the first 6 bytes in to a string so I can compare the bits to determine if it is a gif, but I can't get the bytes in to a string.</p>
  
  <pre><code>int i;
              int comp1, comp2;
  
              for (i = 0; i != 6; i++)
              {
                  string gifStr;
                  fin.read((char*)&amp;a_file, i);
  
                  gifStr(&amp;a_file, i);
              }
  
              string gifStr1 = "GIF87a";
              string gifStr2 = "GIF89a";
  
              comp1 = strcmp( , gifStr1);
  
              if (comp1 == 0)
              {
                  cout &lt;&lt; a_file.name &lt;&lt; " has a .gif extension" &lt;&lt; endl;
              }
  
              comp2 = strcmp( , gifStr2);
  
              if (comp2 == 0)
              {
                  cout &lt;&lt; a_file.name &lt;&lt; " has a .gif extension" &lt;&lt; endl;
              }   
  </code></pre>
  
  <p>Sorry, this website confuses me a little bit on responses and things like that... Haha.</p>