0

I am for some reason unable to generate any jpgs with my code; I think there's a mistake in the logic somewhere? Would be very grateful for some hints! Thank you!

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

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

    if (argc != 1)
    {
        printf("One argument only please!\n");
        return 1;
    }
    char *address = argv[1];
    FILE *input = fopen(address, "r");
    char result[8]; 
    FILE *output = fopen(result, "r");
    if (input == NULL)
    {
        printf("Could not open file.\n");
        return 1;
    }
    //create buffer
    
    int size = 512;
    unsigned char buffer[size];
    bool open_jpeg = false;
    int number_of_jpegs = 0;
    //check first digits are in buffer
    fread(&buffer, size, 1, input);
    fopen(address, "r");
    while (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
    {
        if (open_jpeg == false)
        {
            fopen(result, "w");
            fwrite(result, size, 1, output);
            open_jpeg = true;
            number_of_jpegs ++;
        }
        else 
        {
            fwrite(result, size, 1, output);
        }
    sprintf(result, "%03i.jpg", number_of_jpegs);
    fclose(output);
    open_jpeg = false;
    }
    return  0;
}
1

Maybe the code should use the while statement to actually read data and use an if statement inside the while loop to look for signatures?

You must log in to answer this question.

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