0

I'm working on the Recover problem, and I'm unable to fix this error I'm getting when trying to compile: "while loop has empty body"

If you look at my code I think what should be happening is the while loop executes so long as there are at least 512 bytes left in the input file. Since fread() returns how many things were read, and a bloc size is 512, it should return 512 until the last bloc in the input file is reached.

But the error I'm getting makes it seem like the while loop doesn't execute at all -- any hints?

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



#define BUFFER_SIZE 512

int main(int argc, char *argv[])
{
    //check for proper usage
    if (argc != 2)
    {
        printf("one input file required\n");
        return 1;
    }

    //open the memory card file
    FILE *image = fopen(argv[1], "r");

    //make sure file opens
    if (image == NULL)
    {
        return 2;
    }

    //create buffer
    unsigned char buffer [BUFFER_SIZE];

    //initialize output file counter to 0
    int file_number = -1;

    //create empty file for jpg
    FILE *photo = NULL;


    //iterate over blocs of memory until end of card is reached
    while ((fread(&buffer, 1, 512, image)) == 512);
    {
        //check if new image
        if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
        {
            file_number++;

            //create a string for the name of the new jpg
            char filename[7];

            //name the new jpg according to the current file number
            sprintf(filename, "%03i.jpg", file_number);

            //open new jpg file
            photo = fopen(filename, "w");

            //write first bloc of new jpg
            fwrite(buffer, sizeof(unsigned char), 512, photo);
        }

        //write bloc of data if the first image file has been reached
        else if (file_number != -1)
        {
            fwrite(&buffer, sizeof(unsigned char), 512, photo);
        }
    }
}

'''

1 Answer 1

1

Delete the semicolon at the end of the line. It marks the end of the while loop, just like a closing curly brace.

You must log in to answer this question.

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