0

I've been working on RECOVER from PSET4. I'm able to compile and it creates all 50 images. When I try to open any of the images I get an error saying "invalid or unsupported image format". I've tried to find similar issues online but can't get anything to work. Any ideas on why this happens? Thank you so much for any help.

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


#define BLOCKSIZE 512
typedef uint8_t BYTE;

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

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

    if (!file)
    {
        printf("Could not open file\n");
        return 1;
    }


    // read file 512 bytes at a time
    BYTE buffer[BLOCKSIZE];

    // count number of images recoverd
    int jpgAmount = 0;

    // jpeg name storage
    char filename[8];

    // pointer
    FILE *openFile = NULL;

    while (fread(buffer, sizeof(BYTE), BLOCKSIZE, file) || feof(file) == 0)
    {

        //check first 4 bytes
        if (buffer[0] == 0xff &&
            buffer[1] == 0xd8 &&
            buffer[2] == 0xff &&
            (buffer[3] & 0xf0) == 0xe0)
        {

            if(openFile != NULL)
            {
                fclose(openFile);
            }

            // name file
            sprintf(filename, "%03i.jpg", jpgAmount);

            // open file
            openFile = fopen(filename, "w");

            // interate number of files collected
            jpgAmount ++;


        }

        if (openFile != NULL)
        {
            // if not a new file, continue writing
            fwrite(openFile, BLOCKSIZE, 1, openFile);
        }
    }

    // close any remaining files
    fclose(openFile);
    fclose(file);


    return 0;

}
1

See anything wrong here?

        fwrite(openFile, BLOCKSIZE, 1, openFile);

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

| improve this answer | |
  • Awesome, thanks Cliff! – Alan W Jul 4 at 21:36

You must log in to answer this question.

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