I am working on recover in problem set 4. However, after trying out different stuff, the code itself compiles but I get a segmentation fault and the outcome is only one jpg file called 000.jpg. I am lurking on the forum to find a sollution that would help but so far I have had zero success.
// Opening an external file to read
FILE *file = fopen(argv[1], "r");
// Checking for NULL values
if (file == NULL)
{
// If any, print an error message
fprintf(stderr, "Could not open, try again!\n");
return 2;
}
FILE *img = NULL;
// A counter for the file names of the restored JPEG images
int counter = 0;
// An unsigned char is used because of the if check
unsigned char *buffer = malloc(512);
// A boolean to check if the start of a new JPEG file is found
bool jpgStartFinder;
// If the size of a JPEG block is not 512, this would be the end of the loop and the files
// Also reads 512 bytes into the buffer
while(fread(buffer, 512, 1, file) == 1)
{
// A check for to see if the bytes represent the start of a JPEG image
if (buffer[0] == 0xff &&
buffer[1] == 0xd8 &&
buffer[2] == 0xff &&
(buffer[3] & 0xf0) == 0xe0)
{
// An array serving for a temporary storage for the filename
char filename[8];
// A check to see whether the program has already found a JPEG
if (jpgStartFinder == false)
{
// If it has not found a JPEG before, it will start writing a new one
// Creating a jpg file with the name corresponding to the counter
sprintf(filename, "%03i.jpg", counter);
// Opening the file for writing
img = fopen(filename, "w");
// Plus one to the counter so that the next JPEG file is with a different name
counter++;
// Reading files from buffer and writing them in the array for writing files
fwrite(buffer, *buffer, 1, img);
}
else if (jpgStartFinder == true)
{
// If it has found a JPEG before, close the previous file and start writing a new one
fclose(img);
// Creating a jpg file with the name corresponding to the counter
sprintf(filename, "%03i.jpg", counter);
// Opening the file for writing
img = fopen(filename, "w");
// Plus one to the counter so that the next JPEG file is with a different name
counter++;
// Reading files from buffer and writing them in the array for writing files
fwrite(buffer, 512, 1, img);
}
jpgStartFinder = true;
//// Freeing the memory allocation to prevent memory leak
free(buffer);
}
// Not the start of a JPEG file
else
{
// A check to see whether the program has already found a JPEG
if (jpgStartFinder == false)
{
// If it has not found a JPEG before, discard the 512 bytes and go to the start of the loop
continue;
}
else if (jpgStartFinder == true)
{
// If it has found a JPEG before, write the bytes here
// Reading files from buffer and writing them in the array for writing files
fwrite(buffer, 512, 1, img);
}
}
}
// Closing the card
fclose(file);
}
Thanks in advance!