0
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

#define infile "card.raw"

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

    uint8_t buffer[512];
    uint8_t startcheck1[4] = {0xff, 0xd8, 0xff, 0xe0}; 
    uint8_t startcheck2[4] = {0xff, 0xd8, 0xff, 0xe1}; 
    char outfile[8] = {0};
    int filecounter = 0;
    int fileopen = 0;
    FILE* outptr = NULL;

    // open input file 
    FILE* inptr = fopen(infile, "r");
    if (inptr == NULL)
    {
        printf("Could not open %s.\n", infile);
        return 2;
    }

    // reads until EOF
    while(fread(&buffer, sizeof(buffer), 1, inptr) == 1)
    {
        fread(&buffer, sizeof(buffer), 1, inptr);

        int counter = 0;

        // compares the first 4 bytes
        for (int i = 0; i < 4; i++)
        {
            if (buffer[i] == startcheck1[i] || buffer[i] == startcheck2[i])
            {
                counter += 1;   
            }
            else
            {
                break;
            }
        }

        // if the 4 bytes are a jpg signatures, writes the file
        if (counter == 4)
        {
            // closes the file, if there was one open
            if (fileopen != 0)
            {
                fclose(outptr);
            }

            filecounter +=1;

            sprintf(outfile, "%03d.jpg", filecounter);
            outptr = fopen(outfile, "w");
            if (outptr == NULL)
            {
                fclose(inptr);
                fprintf(stderr, "Could not create %s.\n", outfile);
                return 3;
            } 
            fileopen = 1; 
        }

        if (fileopen == 1)
        {
            fwrite(&buffer, sizeof(buffer), 1, outptr);
        }

    }   
}

I'm stuck! This is my code, it compiles, runs and produces 16 files from 001.jpg to 016.jpg but there are nothing on the jpg files, I mean, when I try to open them with the image viewer, I see an error message, any ideas on what i'm doing wrong?

Thanks!!

1
while(fread(&buffer, sizeof(buffer), 1, inptr) == 1)
{
    fread(&buffer, sizeof(buffer), 1, inptr);

You are reading a second block before processing the first one. That means that you are processing only half of the data and corrupting the files.

Also, the first file should be 000.jpg, not '001.jpg'.

If this answers your question, please click on the check to accept. Let's keep up on forum housekeeping. ;-)

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .