C program to check whether the file is JPEG file or not

In this blog post, I will teach you to how to write a C program that inputs a file and detects whether the file is JPEG or not.

Steps to check whether the file is JPEG or not :

Step 1:- Give the path and name of the file which you want to check

Step 2: Open and read the file.

Step 3: Check the starting 3 bytes of the file that you have read. If the first, second, and third byte of the given file is 0xff, 0xd8, and 0xff respectively then the given file is JPEG file. Otherwise, it is not a JPEG File.

 

Below is the C code to check whether the given file is JPEG or not:

//Maximum size of the array
#define MAX_SIZE  4


int isJpegImage(const char *ptr)
{
    //file pointer
    FILE *fp = NULL;
    unsigned char readFileData[MAX_SIZE] = {0};

    //open the file
    fp = fopen(ptr, "r");
    if(fp == NULL)
    {
        printf("Error in opening the file\n");
        exit(1);
    }

    //Read file using fgets
    if(fgets(readFileData,MAX_SIZE,fp) == NULL)
    {
        printf("Error in reading the file\n");
        //close the file
        fclose(fp);
        exit(1);
    }
    //close the file
    fclose(fp);

    // return 1 if file jepg otherwise return 0
    const int hasJpegExtension = (readFileData[0] == 0xff)? ((readFileData[1] == 0xd8)
                                 && (readFileData[2] == 0xff)):0;

    return hasJpegExtension;
}

 

Let’s see a driver code to check how the above function is work. I have already put “aticleworld.jpeg” in my project directory.

/*

C program to check whether the file is JPEG file or not
*/
#include <stdio.h>
#include <string.h>


//Maximum size of the array
#define MAX_SIZE  4


int isJpegImage(const char *ptr)
{
    //file pointer
    FILE *fp = NULL;
    unsigned char readFileData[MAX_SIZE] = {0};

    //open the file
    fp = fopen(ptr, "r");
    if(fp == NULL)
    {
        printf("Error in opening the file\n");
        exit(1);
    }

    //Read file using fgets
    if(fgets(readFileData,MAX_SIZE,fp) == NULL)
    {
        printf("Error in reading the file\n");
        //close the file
        fclose(fp);
        exit(1);
    }
    //close the file
    fclose(fp);

    // return 1 if file jepg otherwise return 0
    const int hasJpegExtension = (readFileData[0] == 0xff)? ((readFileData[1] == 0xd8)
                                 && (readFileData[2] == 0xff)):0;

    return hasJpegExtension;
}

int main()
{
    const char * pFileName = "aticleworld.jpeg";

    const int retVal = isJpegImage(pFileName);

    // If the given file is a JPEG file
    if (retVal)
    {
        printf("This Image is "
               "in JPEG format!\n");
    }
    else
    {
        printf("No, This image is not JPEG\n");
    }


    return 0;
}

Output: This Image is in JPEG format!

 

If you want you can give the file as command-line arguments. You need to modify the driver code a little bit.  See the changes to take the file as input from the command line.

/*

C program to check whether the file is JPEG file or not
*/
#include <stdio.h>
#include <string.h>


//Maximum size of the array
#define MAX_SIZE  4


int isJpegImage(const char *ptr)
{
    //file pointer
    FILE *fp = NULL;
    unsigned char readFileData[MAX_SIZE] = {0};

    //open the file
    fp = fopen(ptr, "r");
    if(fp == NULL)
    {
        printf("Error in opening the file\n");
        exit(1);
    }

    //Read file using fgets
    if(fgets(readFileData,MAX_SIZE,fp) == NULL)
    {
        printf("Error in reading the file\n");
        //close the file
        fclose(fp);
        exit(1);
    }
    //close the file
    fclose(fp);

    // return 1 if file jepg otherwise return 0
    const int hasJpegExtension = (readFileData[0] == 0xff)? ((readFileData[1] == 0xd8)
                                 && (readFileData[2] == 0xff)):0;

    return hasJpegExtension;
}

int main(int argc, char* argv[])
{

    // If number of command line
    // argument is not 2 then return
    if (argc == 2)
    {

        // Take file as argument as an
        // input and read the file
        const int retVal = isJpegImage(argv[1]);

        // If the given file is a JPEG file
        if (retVal)
        {
            printf("This Image is "
                   "in JPEG format!\n");
        }
        else
        {
            printf("No, This image is not JPEG\n");
        }
    }
    else
    {
        printf("Enter Valid command");
    }


    return 0;
}

Output:

C code to Check whether image is jpeg or not

 

 

Recommended Articles for you:

Leave a Reply

Your email address will not be published. Required fields are marked *