▲ 2 r/cs50
Want a help in recover problem set
Every time I run check 50 in the terminal it always tells me that :
:( recovers 000.jpg correctly
000.jpg not found
:( recovers middle images correctly
001.jpg not found
:( recovers 049.jpg correctly
049.jpg not found
:| program is free of memory errors
can't check until a frown turns upside down
But I don't know how to solve that yet . Can anyone help me in that?
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
int main(int argc, char *argv[])
{
// Accept a single command-line argument
if(argc != 2)
{
printf("usage: ./recover FILE\n");
return 1;
}
// Open the memory card
FILE *card = fopen(argv[1], "r");
if(card == NULL)
{
printf("Couldn't open the file\n");
}
//Create a buffer for a block of data
uint8_t buffer[512];
// While there's still data left to read from the memory card
char *recard = malloc(3 * sizeof(char));
int i = 0;
int if1st = 0;
FILE *img;
while(fread(buffer, 1, 512,card) == 512)
{
// Create JPEGs from the data
if(buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
{
if(if1st == 0)
{
sprintf(recard, "%03i.jbg", i);
img = fopen(recard, "w");
fwrite(buffer, 1, 512, img);
i++;
if1st = 1;
}
else
{
fclose(img);
sprintf(recard, "%03i.jbg", i);
img = fopen(recard, "w");
fwrite(buffer, 1, 512, img);
i++;
}
}
}
fclose(card);
free(recard);
}
u/Beautiful_Welder5374 — 2 days ago