My code passes 000.jpg check but misses on middle and end jpgs.
:( recovers middle files correctly :( recovers last file correctly
I looked at the hex code for the jpegs and it seemed fine, the headers were all correct, except that the middle and end jpegs were all ending in several lines of zeros bytes. The first jpeg ended with a few zeros bytes, but not that many. I don't know if that has anything to do with my problems with this. I assume that the jpegs don't all have a number of bytes that are divisible by 512.
Final bytes of 000.jpg:
d900 0000 0000 0000 0000 0000 0000 0000
All the others ended with several lines of zeros.
EDIT: I have re-written my code to count the bytes in fread, but it appears I didn't use that correctly. I'll also post the code that worked but didn't pass check50 at the end.
Broken code after adding "result" variable:
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
typedef uint8_t byte;
int main(int argc, char* argv[])
{
// ensure proper usage
if (argc != 2)
{
fprintf(stderr, "Usage: ./recover infile\n");
return 1;
}
// create pointer to data file
char* infile = argv[1];
// open input file
FILE *inptr = fopen(infile, "r");
// if can't open file
if (inptr == NULL)
{
fprintf(stderr, "Could not open %s.\n", infile);
return 2;
}
// to name jpg outfiles consecutively
int filecounter = 0;
int block = 512;
// to assign 512 bytes to buffer
byte *buffer = malloc(block);
char filename[8];
// initialize img as NULL pointer
FILE *img = NULL;
// to use for testing iterations
// int testcounter = 0;
// if end of file not yet reached
while (!feof(inptr))
{
// to read infile to buffer and determine size
size_t result = fread(buffer, 512,1, inptr);
// if at start of jpg
if (buffer[0] == 0xff &&
buffer[1] == 0xd8 &&
buffer[2] == 0xff &&
(buffer[3] & 0xf0) == 0xe0)
{
// to see if jpg already created and to close file so new can be
created
if (img != NULL)
{
fclose(img);
}
// create new file to hold image
sprintf(filename, "%03i.jpg", filecounter);
// open new file to write the data
img = fopen(filename, "w");
// write data to file
fwrite(buffer,sizeof(buffer), 1,img);
// to increase count for naming next jpg
filecounter++;
}
else if(filecounter > 0)
{
if (result == 512)
{
fwrite(buffer,sizeof(buffer),1,img);
}
else
{
// only fwrite actual size
fwrite(buffer,result,1,img);
}
}
}
fclose(img);
free(buffer);
return 0;
}
My original code is identical to above except:
// to read infile to buffer and determine size
fread(buffer, 512,1, inptr);
// if at start of jpg
if (buffer[0] == 0xff &&
buffer[1] == 0xd8 &&
buffer[2] == 0xff &&
(buffer[3] & 0xf0) == 0xe0)
{
// to see if jpg already created and to close file so new can be created
if (img != NULL)
{
fclose(img);
}
// create new file to hold image
sprintf(filename, "%03i.jpg", filecounter);
// open new file to write the data
img = fopen(filename, "w");
// write data to file
fwrite(buffer,sizeof(buffer), 1,img);
// to increase count for naming next jpg
filecounter++;
}
else if(filecounter > 0)
{
// to add to opened image
fwrite(buffer,result,1,img);
}
TODO
line is red if the test failed, green if it passes. It doesn't give additional info about what is wrong (unfortunately). I think you'd need to post the code to get further assistance.