0

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;

}
3
  • My crystal ball is in the shop. Can you edit the question and post code?
    – Cliff B
    Nov 18, 2016 at 20:22
  • Now, can you remove the image and cut and paste actual code so that it can be tested? Nobody wants to transcribe code from an image.
    – Cliff B
    Nov 18, 2016 at 21:13
  • Sorry. Is this ok ?
    – Charlotte
    Nov 20, 2016 at 10:47

1 Answer 1

0

You need to read this part of the pset specification again:

enter image description here

If this solves your problem, please click the green check mark to accept the answer.

1
  • 1
    Ah,I see. Check50 doesn't give the name of the file.Thanks. It works.
    – Charlotte
    Nov 20, 2016 at 22:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .