C program to check whether the file is JPEG file or not
Last Updated :
08 Jun, 2020
Write a C program which inputs a file as a command-line arguments and detects whether the file is JPEG(Joint Photographic Experts Group) or not.
Approach:
- We will give an image as a command line argument while executing the code.
- Read the first three bytes of the given image(file).
- After reading the file bytes, compare it with the condition for JPEG file i.e., if the first, second, and third byte of the given file is 0xff, 0xd8, and 0xff respectively then the given file is JPEG file.
- Else it is not a JPEG File.
Below is the implementation of the above approach:
#include <ctype.h>
#include <stdio.h>
#include <string.h>
int main( int argc, char * argv[])
{
if (argc != 2) {
return 1;
}
FILE * file = fopen (argv[1], "r" );
if (file == NULL) {
return 1;
}
unsigned char bytes[3];
fread (bytes, 3, 1, file);
if (bytes[0] == 0xff
&& bytes[1] == 0xd8
&& bytes[1] == 0xff) {
printf ( "This Image is "
"in JPEG format!" );
}
else {
printf ( "No" );
}
return 0;
}
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...