Determining the extension type of an image file using binary

Hello, 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...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
      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;
                }
        
            
        }
    }
Topic archived. No new replies allowed.