So I think I've nearly finished but I've come upon a bit of roadblock. That is all my photo are invalid. I looked at all the threads I could and couldn't find out why.
My Code:
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#define FAT_BLOCK_SIZE 512
// I figured out how to get block size from here
"https://cs50.stackexchange.com/questions/28357/pset-4-
recover-help-with-reading-512-byte-blocks"
int main(int argc,char *argv[])
// buffer_ptr[0] == 0xff && buffer_ptr[1] == 0xd8 && buffer_ptr[2] == 0xff
&& (buffer_ptr[3] &
0xf0)==0xe0) while(fread(&buffer_ptr,512,1,infile)
{
if(argc != 2)
{
return 1;
fprintf(stderr,"This function only accepts one file argument, the memory
card");
}
// open memory card file
FILE *infile = fopen(argv[1],"r");
if(infile == NULL)
{
return 2;
fprintf(stderr,"Failed to open file");
}
uint8_t *buffer_ptr = malloc(FAT_BLOCK_SIZE);
int jpg_bool = 1;
int num_of_jpgs = 0;
char filename[8];
FILE *img = NULL;
while(fread(buffer_ptr,1,512,infile) == 512)
{
if(buffer_ptr[0] == 0xff && buffer_ptr[1] == 0xd8 && buffer_ptr[2] == 0xff && (buffer_ptr[3] & 0xf0)==0xe0)
{
if(img != NULL)
{
fclose(img);
}
// open a new jpg
sprintf(filename,"%03i.jpg",num_of_jpgs);
img = fopen(filename,"w");
// write block
fwrite(&buffer_ptr,1,512,img);
jpg_bool = 0;
num_of_jpgs++;
}
if(jpg_bool == 0)
{
// writes 512 bytes until new jpg found
fwrite(&buffer_ptr,1,512,img);
}
}