I'm sorry to do this but I'm fairly lost with this pset3 recover for cs50. I've been researching for hours and am no closer to figuring out how to utilize FILE *outptr = fopen and sprintf(filename, "%03i.jpg in order to make new files and name them. Also, my logic for making this work is totally not figured out yet as well. I'm feeling pretty defeated. If any kind soul could take a quick gander and maybe push me in a direction of some kind, I'd really appreciate it.
My code compiles but basically doesn't do anything. Thanks guys. Appreciate it.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cs50.h>
int main(int argc, char *argv[])
{
//check for proper input
if (argc != 2 || strcmp(argv[1], "card.raw") != 0)
{
fprintf(stderr, "Usage: ./recover image\n");
return 1;
}
// open input file
FILE *inptr = fopen("card.raw", "r");
if (inptr == NULL)
{
fprintf(stderr, "Could not open %s.\n", "card.raw");
return 2;
}
// keeps track of jpegs found
int jpegCount = 0;
//temp storage
int buffer[512];
//read 512 chunks
fread(&buffer, 512, 1, inptr);
//repeat until EOF.
while (fread(buffer, 512, 1, inptr) == 1)
{
//read 512 chunks
fread(&buffer, 512, 1, inptr);
//use this to recognize the start of jpeg
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
{
char filename[8];
//open new jpeg and write - x means return null if it already exists
FILE *outptr = fopen(filename, "wx");
//make new filename for each jpeg
sprintf(filename, "%03i.jpg", jpegCount);
//write a 512 block to new file
fwrite(inptr, 512, 1, outptr);
//read next chunk
fread(buffer, 512, 1, inptr);
//is the next part a beginning of a jpeg?
if (!(buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0))
{
//write another 512 block to file
fwrite(inptr, 512, 1, outptr);
}
//close the file
fclose(inptr);
}
}
fclose(inptr);
}