I need some tips! I'm stuck on pset4 recover with a strange error message on check50, which returns:
:) recover.c exists
:) recover.c compiles
:( recovers 000.jpg correctly \ expected 000.jpg to exist
:( recovers middle files correctly \ expected 001.jpg to exist
:( recovers last file correctly \ expected 015.jpg to exist
However, when I run the program I can get all the jpgs and they show correct pictures. I have checked the name of jpg, it is three digit numbers.I wrote those codes to define the title:
char title[8];
sprintf(title,"%03d.jpg",i);
Does anyone know what the problem is? this is the code:
#include<cs50.h>
#include<stdlib.h>
#include <stdio.h>
#include <stdint.h>
uint8_t buffer[512];
int main(int argc, char* argv[])
{
if (argc != 2)
{
return 0;
}
char* infile = argv[1];
FILE* inptr = fopen(infile, "r");
if (inptr == NULL)
{
printf("Could not open %s.\n", infile);
return 1;
}
int i = 0;
FILE* img = NULL;
while(fread(buffer,512,1,inptr)==1)
{
while (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff&& 0xef >= buffer[3] && 0xe0 <= buffer[3])
{
char title[8];
sprintf(title,"%03d.jpg",i);
if(img != NULL)
fclose(img);
img = fopen(title,"a");
fwrite(buffer, 512, 1, img);
while(fread(buffer,512,1,inptr)==1)
{
if(buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff&& 0xef >= buffer[3] && 0xe0 <= buffer[3])
{
i = i+1;
break;
}
fwrite(buffer, 512, 1, img);
}
}
}
fclose(img);
fclose(inptr);
return 0;
}