0

Im about to finish pset4, currently on recover! Everything is ok at the first look, program compiles, however, there is a problem when I run check50. As you can see from the title, something is off with memory. I tried to fclose() everything possible, typed free(), but still this problem. Could you please help me, because that is the only issue I have(( Here's the code!!!

#include <stdlib.h>
#include <stdint.h>
typedef uint8_t BYTE;

int main(int argc, char *argv[])
{
    if(argc!= 2)
    {
        printf("Usage: ./recover IMAGE\n");
        return 1;
    }
    FILE *input_file = fopen(argv[1], "r");
    if (input_file == NULL)
    {
        printf("Usage: ./recover IMAGE\n");
        return 2;
    }
    unsigned char buffer[512];
    int counter = 0;
    FILE *output_file = NULL;
    char *filename = malloc(8 * sizeof(char));
    while (fread(buffer, sizeof(char), 512, input_file))
    {
         if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
         {
            if (counter > 0)
            {
                fclose(output_file);
            }

            sprintf(filename, "%03i.jpg", counter);
            output_file = fopen(filename, "w");
            counter++;
         }

         if (output_file != NULL)
         {
            fwrite(buffer, sizeof(char), 512, output_file);
         }
    }
    free(filename);
    return 0;
}

1 Answer 1

0

you aren't closing input_file. you are closing output_file but doing so in a very convoluted way...

You must log in to answer this question.

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