I can create 49 images inside of my loop, but I'm missing something when I write to file. The images are only partly showing. I watched the video three times where Brian explains how to approach this, and most of it makes sense, but I'm not completely grasping the process. How big is this file and why exactly am I writing 1 byte at a time? What am I writing to the memory card?
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
int main(int argc, char *argv[])
{
// If your program is not executed with exactly one command-line argument, it should remind the user of correct usage, and main should return 1.
if (argc != 2)
{
fprintf(stderr, "Usage: ./recover image\n");
return 1;
}
// Open memory card
FILE *file = fopen(argv[1], "r");
// If the forensic image cannot be opened for reading, program should inform the user, and main should return 2.
while (!file || file == NULL)
{
fprintf(stderr, "File is either an invalid type, or it cannot be opened\n");
return 2;
}
// img pointer initially set to NULL
FILE *img = NULL;
// filename needs to have xxx.jpg length + 1
char filename[8];
// Create a 512 BYTE block for memory
unsigned char byte[512];
// Set a counter to add num to image name and increment in loop to automate naming
int counter = 0;
// Read 512 bytes into a buffer of filename "file" 1 byte at a time
// fread(data, size, number, inptr);
while (fread(byte, 512, 1, file))
{
// Look at jpg signature in the first four bytes for a jpeg file
if (byte[0] == 0xff && byte[1] == 0xd8 && byte[2] == 0xff && (byte[3] & 0xf0) == 0xe0)
{
// If start of new JPEG
// sprintf(filename, "%03i.jpg", 2); counter
sprintf(filename, "%03i.jpg", counter);
// Open a new jpg file after naming it
// Create a new file to add data to ###.jpg starting at 000.jpg
img = fopen(filename, "w");
// fwrite (data, size, number, outptr);
fwrite(byte, sizeof(byte), 512, img);
// Add to counter each time an image is written to
counter++;
if (feof(img))
{
fclose(img);
}
}
else
{
//???
}
}
}```