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.
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?
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...
getDir();
ifstream fin;
_finddata_t a_file;
intptr_t dir_handle;
dir_handle = _findfirst("*.*", &a_file);
//if (dir_handle == -1)
//{
//return;
//}
while (_findnext(dir_handle, &a_file) == 0);
{
fin.open(a_file.name, ios::in | ios::binary);
if (!fin)
{
cout << endl << "Could not open the file."
<< " Attempting to open the next file." << endl;
return false;
}
else
{
cout << "Files opened successfully."
<< " Processing through the directory." << 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 << a_file.name << " has a .gif extension" << endl;
return true;
}
}
}
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.
int i;
int comp1, comp2;
for (i = 0; i != 6; i++)
{
string gifStr;
fin.read((char*)&a_file, i);
gifStr(&a_file, i);
}
string gifStr1 = "GIF87a";
string gifStr2 = "GIF89a";
comp1 = strcmp( , gifStr1);
if (comp1 == 0)
{
cout << a_file.name << " has a .gif extension" << endl;
}
comp2 = strcmp( , gifStr2);
if (comp2 == 0)
{
cout << a_file.name << " has a .gif extension" << endl;
}
Sorry, this website confuses me a little bit on responses and things like that... Haha.