Diagnostic in Code Retrieving JPG’s from File

Asked

Viewed 22 times

1

In a course exercise I have to write a program that recovers JPG images from a file. The code below is not ready, but it should find the signatures of JPG (0xffd8ffe0 or 0xffd8ffe1) inside the file jumping from 512 to 512 bytes (FAT) and print a text. Doubt:

  • The code doesn’t work, I can’t diagnose, which are the problems?

Filing cabinet: https://github.com/kaarejoergensen/pset5/blob/master/jpg/card.raw

Code:

/**********************************
 * recover.c
 * CC50 Pset5
 * Recupera imagens JPG. Matheus.
 *********************************/

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

int
main(void)
{
    // Boas Vindas
    printf("\033[1;30;47mPOWERED BY RAMALHOLIVEIRA\033[0m\nIniciando operação.\n");

    // Abrir Arquivo
    FILE *fpr = fopen("card.raw", "r");
    if (fpr == NULL)
    {
        printf("\033[31mErro ao abrir o arquivo.\033[0m\n");
        return 1;
    }

    // Variáveis
    uint32_t cursor = 0;
    uint32_t assinatura = 0x00;
    FILE *saida = fopen("saida", "w");
    int n = 1;

    // Se 'fread' retornar 0 o loop acaba
    do
    {
        // Lê 4 bytes e armazena em 'assinatura'
        cursor = fread(&assinatura, 4, 1, fpr);
        fwrite(&assinatura, 4, 1, saida);
        if (assinatura == 0xffd8ffe0 || assinatura == 0xffd8ffe1)
        {
            printf("%d Encontrado!\n", n);
            ++n;
        }
        fseek(fpr, 512, SEEK_CUR);
    }
    while (cursor != 0);

    // Isso é tudo, pessoal :)
    fclose(fpr);
    fclose(saida);
    printf("Isso é tudo, pessoal.\n");
    return 0;
}
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.