fellow programmers!
I've been stuck for days trying to solve this problem, but i can't really seem to find the error in this code, which opens all the images but they do not match when i use the check50 command although it creates all the image files and iterates correctly. I'd be very grateful if someone could help me out :)
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
typedef uint8_t BYTE;
int main(int argc, char *argv[])
{
if (argc != 2)
{
printf("Usage: ./recover image");
return 1;
}
//if the command line arguments are valid, we open the memory card
FILE *f = fopen(argv[1], "rb");
//Tests if the file was opened successfully
if(f == NULL)
{
printf("./recover card.raw");
return 1;
}
FILE *img = NULL;
char image[10];
int ImageCounter = 0;
do
{
//Declare a buffer pointer to where we will store each chunk of memory to read
BYTE buffer[512];
fread(buffer, sizeof(BYTE) , 512, f);
//check if it is a JPEG (header conditions).
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
{
printf("entrou no if condicional\n");
//If it's the first JPEG, creates a file and writes it to it
if (ImageCounter == 0)
{
sprintf(image, "%03i.jpg", ImageCounter);
//creates a name for the file that we will create, corresponding to the ith iteration
img = fopen(image, "w");
fwrite(buffer, sizeof(BYTE), 512, img);
ImageCounter++;
}
//If it isn't the first JPEG, but since it's another header, we need to close the last one, and create a new JPEG
if (ImageCounter > 0)
{
fclose(img);
sprintf(image, "%03i.jpg", ImageCounter);
img = fopen(image,"w");
fwrite(buffer, sizeof(BYTE), 512, img);
printf("escreveu a imagem\n");
ImageCounter++;
}
//this is supposed to store parts of the picture that are not the header
img = fopen(image,"w");
fwrite(buffer, sizeof(BYTE), 512, img);
}
}
while(ImageCounter < 50);
return 0;
}