FFlores
FFlores

Reputation: 11

CS50 Retrieving blank images when writing program to recover jpgs

I am having trouble identifying why I am getting blank images when recovering jps for an assignment in CS50. I read the entire file, write whenever the start of a jpg file is found, and close whenever the end of a jpg file is found. I create the total of 50 images, but all of them are blank. Could someone help push me in the right direction? This is my code:

const int BLOCK = 512;

    uint8_t buffer[BLOCK];
    char filename[50][8] = { };
    int counter = 0;
    while (fread(buffer, BLOCK, 1, input) == 1)
    {
        sprintf(&filename[counter][0], "%03i.jpg", counter);
        FILE *img = fopen(&filename[counter][0], "w");
        if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer [2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
        {
            if ((buffer[511] & 0xf0) != 0xe0)
            {
                fwrite(buffer, BLOCK, 1, img);
                counter++;
            }
            else
            {
                fclose(img);
            }
        }
    }

I made the update here:

while (fread(buffer, BLOCK, 1, input) == 1)
    {
        sprintf(filename, "%03i.jpg", counter);
        FILE *img = fopen(filename, "w");
        if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer [2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
        {
            if ((buffer[511] & 0xf0) != 0xe0)
            {
                fwrite(buffer, BLOCK, 1, img);
            }
            else
            {
                fclose(img);
                counter++;
            }
        }
    }

Upvotes: 0

Views: 306

Answers (1)

FFlores
FFlores

Reputation: 11

This question is now solved. My whole logic was flawed and I wasnt writing anything, I was just opening files. Bellow is my reworked code, its funny how overnight everything just clicked and was able to write it in like 10 minutes!

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

const int BLOCK = 512;

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        printf("Usage: ./recover card.raw\n");
        return 1;
    }

    FILE *input = fopen(argv[1], "r");
    if (input == NULL)
    {
        printf("Could not open file.\n");
        return 1;
    }

    uint8_t buffer[BLOCK];
    char filename[8] = { };
    int counter = 0;
    FILE *img;
    while (fread(buffer, BLOCK, 1, input) == 1)
    {
        if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer [2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
        {
            sprintf(filename, "%03i.jpg", counter);
            img = fopen(filename, "w");
            if (counter == 0)
            {
                fwrite(buffer, BLOCK, 1, img);
                counter++;
            }
            else
            {
                fclose(img);
                img = fopen(filename, "w");
                fwrite(buffer, BLOCK, 1, img);
                counter++;
            }
        }
        else if (counter != 0)
        {
            fwrite(buffer, BLOCK, 1, img);
        }
    }
    fclose(input);
    fclose(img);
}

Upvotes: 1

Related Questions