0

I have started recover.c in pset4.

I have decided to approach the problem step by step, so first task is recovering the first jpeg. enter code here

  FILE* file = fopen("card.raw", "r");
    unsigned char buffer[512];
    while(fread(&buffer, sizeof(buffer), 1, file) != 0){
        fread(&buffer, sizeof(buffer), 1, file);
        if(buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] > 223 && buffer[3] < 240)){
            char* name = "test.jpg";
            FILE* test = fopen(name, "w");
            fwrite(&buffer, sizeof(buffer), 1, test);
            while(buffer[0] != 0xff && buffer[1] != 0xd8 && buffer[2] != 0xff && (buffer[3] > 223 && buffer[3] < 240)){
                fwrite(&buffer, sizeof(buffer), 1, test);
            }
            fclose(test);
        }
    }

Program compiles and works but the first jpeg file is blank! What is going on?

0

Based on the code that was posted, I see a couple problems. First, it has two back to back fread calls - one in the while setup statement and one immediately after it. This will throw away half of the data without processing it.

Next, the inner while statement isn't doing what you think. To get the negative of a complex logical statement, you can't just change == to != on the individual conditions. (Take a semester of digital logic and you'll understand. ) But, you can take the whole logical statement, surround it with parentheses and put a not symbol in front of it.

Having said that, if it were to work, it would be an infinite loop. There's nothing inside the loop code that would cause the test clause to become false. Since there's no read inside that inner while loop, it would just write the same buffer over and over until the system suspended the program for excess disk usage.

This should get you started. If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

| improve this answer | |

You must log in to answer this question.

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