like i said in the title I'm currently working on the pset4 recover problem. Right now my program find all 50 jpeg in card.raw but when I run the check50 it says that the recovered image doesn't match. Anyway here's my code :
#include<stdio.h>
#include<stdlib.h>
#include<stdint.h>
FILE * openoutfile(int a);
int main(int argc, char *argv[])
{
//check for correct number of command line
if (argc != 2)
{
fprintf(stderr, "Usage : ./recover image's file\n");
return 1;
}
//open infile and check if valid
char *infile = argv[1];
FILE *inptr = fopen(infile, "r");
if (inptr == NULL)
{
fprintf(stderr, "Could not open %s.\n", infile);
return 2;
}
/*declare size of memory to check(buffer), counter for program name(a),
int for the first time in the loop(c) and int for the return value of
fread(s)*/
uint8_t buffer[512] = {0x00};
int sizeb = sizeof(buffer);
int sizeu = sizeof(uint8_t);
int a = 0;
int c = 0;
int s = 0;
/*fread until first JPEG is found, open the outfile and start writing on
it*/
while (buffer[0] != 0xff && buffer[1] != 0xd8 && buffer[2] != 0xff &&
(buffer[3] & 0xf0) != 0xe0)
{
s = fread(&buffer, sizeu , sizeb, inptr);//goes throught 1st JPEG?
}
FILE *outptr = openoutfile(a);
fwrite(&buffer, sizeu , sizeb, outptr);
//loop for the rest of the file
while ( s == 512)
{
//first time in the loop, doesn't rewind the pointer for fread
if (c != 0)
fseek(inptr, -1 * (sizeb), SEEK_CUR);
if (c == 0)
c++;
fread(&buffer, sizeu , sizeb, inptr);
/*if a new JPEG is found, close the outfile, increase a by 1, open a
new outfile, start writing on it and read the next buffer for correct
size*/
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff &&
(buffer[3] & 0xf0) == 0xe0)
{
fclose(outptr);
a++;
outptr = openoutfile(a);
fwrite(&buffer, sizeu , sizeb, outptr);
s = fread(&buffer, sizeu , sizeb, inptr);
}
/*if no new JPEG is found, write on the current outfile and read the
next buffer for correct size*/
else
{
fwrite(&buffer, sizeu , sizeb, outptr);
s = fread(&buffer, sizeu , sizeb, inptr);
}
}
fclose(outptr);
fclose(inptr);
return 0;
}
//function to open new outfile
FILE * openoutfile(int a)
{
char outfile[512];
sprintf(outfile, "%03i.jpg", a);
FILE *outptr = fopen(outfile, "w");
return outptr;
}