I am stuck with a segmentation fault after finishing up the code for recover.c. I have read the code over a few times but it seems to make sense to me... So I can't really tell what I'm doing wrong. I have ran valgrind to see if there are any memory leaks. It shows that there might be some error on line 60, but I can't seem to find anything wrong there either. So I was wondering if someone can please help me look over my code and point out what I did wrong.
Thanks! :)
/**
* recover.c
*
* Computer Science 50
* Problem Set 4
*
* Recovers JPEGs from a forensic image.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
int main(int argc, char* argv[])
{
// open memory file
FILE* inptr = fopen("card.raw", "r");
//assigning stuff
char jpgname [8];
int c = 0;
uint8_t buffer[512];
if (inptr == NULL)
{
printf("Could not open file");
return 1;
}
FILE * outptr = NULL;
while(fread(buffer, 512, 1, inptr) == 1)
{
fread(buffer,512,1,inptr);
if( (buffer[0] == 0xff && buffer [1] == 0xd8 && buffer [2] == 0xff && buffer [3] == 0xe0 )|| (buffer[0] == 0xff && buffer [1] == 0xd8 && buffer [2] == 0xff && buffer [3] == 0xe1 ))
{
if (outptr != NULL)
{
fclose(outptr);
}
sprintf (jpgname, "%d.jpg", c);
FILE * outptr = fopen(jpgname, "w");
fwrite (&buffer,512,1,outptr);
fread(&buffer,512,1,inptr);
}
if( (buffer[0] != 0xff && buffer [1] != 0xd8 && buffer [2] != 0xff && buffer [3] != 0xe0 )|| (buffer[0] != 0xff && buffer [1] != 0xd8 && buffer [2] != 0xff && buffer [3] != 0xe1 ))
{
fwrite (&buffer,512,1,outptr);
}
fclose (outptr);
return 0;
}
}