0
I’m with a problem in my code, because Every time I Try to run it I get "Segmentation fault", but I can’t find the error. The code is:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
//Checking if there is only one command-line argument
if (argc != 2)
{
printf("Usage: ./recover image\n");
return 1;
}
//Opening the forensic image and checking if it could be opened
FILE *input = fopen(argv[1], "r");
if (input == NULL)
{
printf("Could not open input\n");
return 1;
}
FILE *image = NULL;
unsigned char buffer[512];
int found = 0;
int nameCount = 0;
char imageName[8];
while (fread(buffer, 512, 1, input) == 1)
{
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && buffer[4] >= 0xe0 && buffer[4] <= 0xef)
{
if (found == 1)
{
fclose(image);
}
sprintf(imageName, "%03d.jpg", nameCount);
image = fopen(imageName, "w");
if (image == NULL)
{
return 2;
}
fwrite(&buffer, 512, 1, image);
nameCount++;
found = 1;
}
else
{
if (found == 1)
{
fwrite(&buffer, 512, 1, image);
}
}
}
fclose(image);
fclose(input);
}
Could someone help me to figure out what to do, Please?