0

My recover program recovers 15 jpg files, 000.jpg through 015.jpg. All files open without issue.

However, check50 fails

:) recover.c exists
:) recover.c compiles
:( recovers 000.jpg correctly
:( recovers 001.jpg through 014.jpg correctly
:( recovers 015.jpg correctly

https://sandbox.cs50.net/checks/ee9daf39c3604acb955e7b19ff95e057

There are no issues compiling, and valgrind shows no data leaks. There are no extraneous print outs to the directory, all files are closed.

I know others have experienced similar check50 issues, but none of the fixes seem to apply. I can't figure out what is wrong.

typedef uint8_t  BYTE;

int main(int argc, char* argv[])
{

FILE* memcardptr = fopen("card.raw", "r");
FILE* outptr = NULL;

int numJpg=0;
BYTE* buffer=malloc(512*sizeof(BYTE));

char* filename=malloc(sizeof("000.jpg"));

while(fgetc(memcardptr) != EOF)//while not end of card
{
    fseek(memcardptr,-sizeof(BYTE), SEEK_CUR);

    fread(buffer, sizeof(buffer),1, memcardptr);

    if ((buffer[0]==0xff)&&(buffer[1]==0xd8)&&(buffer[2]==0xff)&&((buffer[3]==0xe0)||(buffer[3]==0xe1)))
    {
        if(outptr!=NULL)
        {
            fclose(outptr);
        }

        sprintf(filename,"%03d.jpg",numJpg);
        numJpg++;
        outptr = fopen(filename, "w");
    }

    if (outptr!=NULL)
    {
        fwrite(buffer, sizeof(buffer),1, outptr);
    }
}
free(filename);
free(buffer);
fclose(outptr);
// close input file
fclose(memcardptr);
}

I would greatly appreciate any help.

1 Answer 1

1

'killed by server' usually means that it is taking far too long to compile or execute. Since it compiled successfully, it looks like an execution problem. My concern is that check50 is reporting fails on all file creations. That could be an infinite loop. It could also mean that you're opening all the files but not closing them, or waiting till the end to close them all. Having all output files open at once has also been known to cause issues. There could be any number of reasons for the problem, but without seeing any code, it's just guesswork here.

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

8
  • The only infinite loop could be in the while loop shown above - that's the only loop in the program. And I have checked over and over that all files are closed...after executing file close, I printed out "File is closed" just to make sure the code above ran (and I removed this from my code, so, no, I don't have extraneous printfs in the code, which I read on another post could cause the problem). Additionally, I close each file before opening a new one. Commented Nov 3, 2015 at 12:11
  • Looking at it again, I see that your loop appears to be getting one character at a time. There are so many ways that things could go wrong with this and there is no way that anyone can diagnose your problem based on the two lines of code in your question. Why are you reading only one character at a time instead of 512 bytes as described in the problem? If you want any credible assistance, you're going to have to provide much more info and code.
    – Cliff B
    Commented Nov 3, 2015 at 16:56
  • What I am doing is fgetc to find out if I am at the end of the file? I then back up the pointer to the original position. Then I fread into the buffer array of 512 bytes. Then the program continues. I couldn't figure out a way to check for end of file with the buffer. Also, I don't know how much code I am allowed to post without violating honesty code. Commented Nov 3, 2015 at 17:38
  • still can't see the rest of the code!
    – Cliff B
    Commented Nov 3, 2015 at 17:45
  • Should I post all of the code? Commented Nov 3, 2015 at 18:22

You must log in to answer this question.

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