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:
Recommended Articles for you:
- C Program to Find the Length of a String using strlen().
- Best Gifts for the programmer and techies.
- C Programming Courses And Tutorials.
- CPP Programming Courses And Tutorials.
- Python Courses and Tutorials.
- find all prime numbers up to n using trial division and Sieve of Eratosthenes algorithm.
- C Program to find prime factors of a number.
- C Program to print Twin prime numbers between two ranges.
- C program to calculate the power of a number.
- How to find whether a given number is prime number in C?
- sqrt function in C.
- C program to find all roots of a quadratic equation using switch case.
- C program to find the roots of a quadratic equation.