CS50X PSet 4 Recover
I read the first block of data and check whether it is the beginning of a jpeg file. After determining the beginning of the jpeg I then try to write the first block which then causes a segmentation fault.
I have isolated the segmentation fault to writing the first 512 bytes to the output file.
Here is a relevant code sect
// FILE *file = fopen(argv[1], "r");
FILE* file = NULL;
file = malloc(514 * sizeof(char));
file = fopen(argv[1], "r");
printf("count zero = %d\n", count );
// Check if *file is NULL
if (!file)
{
printf("count return = %d\n", count );
return 1;
}
// initialize first_image = 1 for first image
// int first_image=0;
// Read block of 512 bytes
count = fread(bytes, sizeof(char), 512, file);
printf("count = %d\n", count );
while (count != 0)
{
printf("count 52 = %d\n", count );
// check if bytes are the jpg header 0xff 0xd8 0xff and oxe? (jpeg)
if (*bytes[0] == 0xff && *bytes[1] == 0xd8 && *bytes[2] == 0xff && (*bytes[3] & 0xf0) == 0xe0)
{
printf("count 56= %d\n", count );
if (n == 0)
{
// create filename
sprintf(filename, "%03i.jpg", n );
filename = malloc(9 * sizeof(char));
// *filename = malloc(9 * sizeof(char));
// open first file
fileout = malloc(512 * sizeof(char));
fileout = fopen(filename,"w");
// write data to first file
fwrite(&bytes, sizeof(char), count,