-1

My program seems to function correctly, but check50 disagrees. Can anyone please tell me why? Thank you in advance!

#include <stdio.h>
#include <stdlib.h>
#include <cs50.h>
#include <stdint.h>

int main(int argc, char* argv[])
{
    if(argc != 1)
    {
        printf("Usage: ./recover\n");
        return 1;
    }
    // TODO

    FILE* inptr  = fopen("card.raw","r");


    if (inptr == NULL)
    {
        printf("Not a valid file\n");
        return 2;
    }

   uint8_t buffer[512];
   int i = 0;

   while(fread(&buffer,512,1,inptr) ==1)
   {

       if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && 
           (buffer[3] == 0xe0 || buffer[3] == 0xe1))
       {     

           char* name = malloc(sizeof(char) * 8);

           sprintf(name, "%03d.jpg",i);

           FILE* outptr = fopen(name, "w");

           free(name);

           i++;

           do
           {
               fwrite(&buffer, 512, 1, outptr);
               fread(&buffer,512,1, inptr);

           } while(!(buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && 
               (buffer[3] == 0xe0 || buffer[3] == 0xe1)));

           fseek(inptr,-512,SEEK_CUR);
           fclose(outptr);
        }
    }

    fclose(inptr);
    return 0;   
}
1
  • 1
    what error code are you getting from check50? – ronga Nov 16 '15 at 4:07
0

Your code doesn't handle EOF of the input file. Instead, it goes into an infinite loop in the inner do/while loop, writing to the last output file. If the program is terminating, it is either for timing out or, more likely, for making the 015.jpg file so large that it uses up all available file space. Check the file size of your output files against the size of the input file.

If this answers your question, please click the check mark to accept. Let's keep up on forum housekeeping. ;-)

You must log in to answer this question.

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