I am 2 days into PSET 4 Recover. I can get the program to compile successfully without errors but my program is not returning any JPEGs, with no errors to point me in any direction as to what might be the issue. My guess is there's a problem with the logical flow of the program and certain parts of the program are not being reached. Does anybody have any suggestions?
In PSET 4 we are supposed to create a program which inputs a memory card and scans through that memory card for the signatures of lost JPEG files. Once found, these JPEG files then need to be saved to new files to be recovered. There are 50 in total.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
typedef uint8_t byte;
//Function prototype
bool isJPEG(byte *block);
int main(int argc, char *argv[])
{
// Global Variables
int JPEGfound = 0;
int picnumber = 1;
char filename[8];
// Variable Initialization
FILE* img = NULL;
FILE* input = NULL;
// Checks that one command-line argument has been entered, if so opens memory card
if (argc == 2)
{
input = fopen(argv[1], "r");
return 0;
if (input == NULL)
{
printf("Could not open %s", argv[1]);
return 1;
}
}
else
{
printf("Usage: ./recover [___.raw]");
return 1;
}
byte block[512];
// Loop through the file searching for JPEGs until no more data
while (fread(block, sizeof(block), 1, input) == 1)
{
// Iterate through file searching for JPEG headers
if (isJPEG(block) == 0)
{
// First JPEG header found, set JPEG found to 1
if (JPEGfound == 0)
{
JPEGfound = 1;
}
// Already a JPEG in the block. Close it
else
{
fclose(img);
}
// Filename defined by picnumber, iterates with new pn. Open new and write first JPEG inside
sprintf(filename, "%03i.jpg", picnumber);
img = fopen(filename, "w");
fwrite(&block, sizeof(block), 1, img);
picnumber++;
// If JPEG has been found continue writing JPEGS inside
if (JPEGfound == 1)
{
fwrite(&block, sizeof(block), 1, img);
picnumber++;
}
}
}
// End of memory card - close remaining files
fclose(input);
fclose(img);
return 0;
}
bool isJPEG (byte *block)
{
if (block[0] == 0xff && block[1] == 0xd8 && block[2] == 0xff && (block[3] & 0xf0) == 0xe0)
{
return true;
}
else
{
return false;
}
}