Reputation: 1
I have been on this problem all day but I cannot seem to discover what is wrong with my code and why it won't pass on check 50. If I try to make changes I would get a segmentation fault and I cannot seem to figure out what I am missing. I have checked filename at initialization and it has sufficient memory for 8 char characters. I am lost on this one.
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
int main(int argc, char *argv[])
{
if (argc != 2)
{
printf("Usage: ./recover filename.exe\n");
return 1;
}
FILE *file = fopen(argv[1], "r");
if (file == NULL)
{
printf("Error: cannot open file\n");
return 2;
}
typedef uint8_t BYTE;
int counter = 0;
char Filename[8];
FILE *output = NULL;
BYTE buffer[512] = {0};
while(fread(buffer, 512, 1, file) == 1)
{
if(buffer[0] == 0xff || buffer[1] == 0xd8 || buffer[2] == 0xff || (buffer[3] & 0xf0) == 0xe0 || feof(file) == 0)
{
if(output != NULL)
{
fclose(output);
sprintf(Filename,"%03i.jpg", counter);
output = fopen(Filename, "w");
fwrite(buffer, 512, 1, output);
counter++;
}
else
{
sprintf(Filename,"%03i.jpg", counter);
output = fopen(Filename, "w");
fwrite(buffer, 512, 1, output);
counter++;
}
}
if(output != NULL)
{
fwrite(buffer, 512, 1, output);
}
}
fclose(output);
fclose(file);
return 0;
}
Upvotes: 0
Views: 51