can we open .jpeg file using c/c++.

Dani AI

Generated

Short answer: yes — C and C++ can read and decode JPEG files, but a JPEG is a compressed binary image format, so you normally use a decoder library rather than a single built‑in function. was right that it can be done, and was right to point out you need a decoder or a format parser.

Common, practical options:

  • Use a lightweight single-file loader like stb_image (stb on GitHub) for quick tests.
  • Use a full JPEG library such as libjpeg/libjpeg-turbo for production/fast decoding (libjpeg-turbo).
  • Use a higher-level toolkit like OpenCV if you need image processing beyond loading (OpenCV).

If you only need to detect a JPEG before handing it to a library, check the file magic bytes. This minimal C example shows detection (not decoding):

#include <stdio.h>

FILE *f = fopen("image.jpg", "rb");
unsigned char hdr[3];
if (f && fread(hdr, 1, 3, f) == 3) {
    if (hdr[0]==0xFF && hdr[1]==0xD8 && hdr[2]==0xFF) {
        /* likely JPEG */
    }
}
if (f) fclose(f);

For full decoding with libjpeg, the common call sequence is: jpeg_create_decompress, jpeg_stdio_src, jpeg_read_header, jpeg_start_decompress, jpeg_read_scanlines, jpeg_finish_decompress, jpeg_destroy_decompress.

Note about environment: Turbo C++ is an old DOS-era tool and will not work with modern libraries; consider using a current compiler/toolchain (GCC/MinGW, Clang, or Visual Studio) instead. Also watch for opening files in binary mode ("rb"), linking required libraries, and checking return/error codes when decoding.

Recommended Answers

All 3 Replies

Yes

can u tell me the functions which is to be used. i am using turbo c++.

can u tell me the functions which is to be used.

It's not that easy. There aren't any standard functions for processing image formats. You need to use a third party library or manually process the .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.