I must admit I'm having particular trouble in this assignment. Maybe it's the amount of new concepts. I think I have the idea down but I must me missing an error regarding the loop. I've tried debugging to no avail (particularly because it's a lot to look out for and every image has to go through a lot of iterations). If you could point me in the right direction as to where I'm failing I believe that could be enough for me to try and figure it out, much appreciated.
int main(int argc, char *argv[])
{
if (argc != 2)
{
printf("Usage: ./recover image");
return 1;
}
FILE *card = fopen(argv[1], "r");
typedef uint8_t BYTE; // creating a BYTE data type
BYTE buffer[512]; // creating a byte array to read file into
int found = 0;
char filename[7] = "";
FILE *img;
int x;
while (fread(buffer, sizeof(BYTE), 512, card) == 512)
{
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
{
if (found == 0)
{
sprintf(filename, "%03i.jpg", found);
img = fopen(filename, "w");
fwrite(buffer, sizeof(BYTE), 512, img);
found ++;
}
else
{
fclose(img);
sprintf(filename, "%03i.jpg", found);
img = fopen(filename, "w");
fwrite(buffer, sizeof(BYTE), 512, img);
found++;
}
}
else
{
if (found >= 1)
{
fwrite(buffer, sizeof(BYTE), 512, img);
}
}
}
fclose(img);
}
This is what I think I'm doing, if it helps (it's literaly the pseudocode given in the video): // open memory card // repeat until end of card // read 512 bytes into a buffer // if start of new jpg // if first jpeg // create first jpeg // else // close previous, open new // else // if already found jpeg // continue writing to it // close remaining files